Part A Example Write a program to have the user input a number. The person shoul
ID: 3879116 • Letter: P
Question
Part A Example
Write a program to have the user input a number. The person should then output whether the number is divisible by 3. Output the result in an easy to understand format. Document and format your code correctly.
Part A
Write a program to have the user input a number. The program should then output whether the number is even or odd. Output the result in an easy to understand format. Document and format your code correctly.
Part B Example
Write a program that has the user input the lengths of each side of a triangle. The program should then determine if the triangle is scalene, isosceles, equilateral, or no triangle. Output the result in an easy to understand format. Document and format your code correctly.
Part B
sixth edition - Chapter 4, Programming Exercise 8, page 254
Part C Example
Write a program that has the user input a letter grade. The program should then output the numerical value of the grade if valid. Output the result in an easy to understand format. Document and format your code correctly.
Part C
Write a program that has the user input a letter between A and G. The program should then output the military name associated with that letter. Output the result in an easy to understand format. Document and format your code correctly.
a = alpha
b = bravo
c = charlie
d = delta
e = echo
f = foxtrot
g = golf
i need help with this code.
Explanation / Answer
PART A EXAMPLE
import java.util.Scanner;
public class Divisiblity_check
{
public static void main(String[] args)
{
int n;
Scanner s = new Scanner(System.in);
System.out.print("Enter any number:");
n = s.nextInt();
if(n % 3 == 0)
{
System.out.println(n+" is divisible by 3");
}
else
{
System.out.println(n+" is not divisible by 3");
}
}
}
PART A
import java.util.Scanner;
public class Even_or_Odd
{
public static void main(String[] args)
{
int n;
Scanner s = new Scanner(System.in);
System.out.print("Enter any number:");
n = s.nextInt();
if(n % 2 == 0)
{
System.out.println(n+" is an even number");
}
else
{
System.out.println(n+" is an odd number");
}
}
}
PART B EXAMPLE
import java.util.Scanner;
public class Triangle
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
int side1, side2, side3;
//To get the lengths of the sides of the triagle as an input from the user
System.out.println(" To determine the type of triangle please enter the lengths of the sides. " +
" Each length MUST be entered in INCREASING order of size. ");
System.out.println("Side 1: ");
side1 = s.nextInt();
System.out.println("Side 2: ");
side2 = s.nextInt();
System.out.println("Side 3: ");
side3 = s.nextInt();
//To check if it is a triangle, the sum of two smaller sides must be greater than the longest side to be a triangle
if((side1 + side2 > side3))
{
//To check if it an Equilateral triangle, the 3 sides should be of the same length
if((side1==side2) && (side2==side3))
{
System.out.print(" RESULT: An Equilateral Triangle.");
}
//To check if it is an Isosceles triangle, any 2 sides should be of the same length
else if ((side1 == side2) & (side2 != side3) || (side2 == side3) & (side3!= side1))
{
System.out.print(" RESULT: An Isosceles Triangle.");
}
//To check it is a Scalene triangle, all the 3 sides should be of the same length
else if((side1 != side2) & (side2 != side3))
{
System.out.print(" RESULT: A Scalene Triangle.");
}
}
//If the sum of two smaller sides is smaller than the longest side, a triangle cannot be formed
else
{
System.out.print(" RESULT: This is not a triangle");
}
}
}
PART C EXAMPLE
import java.util.Scanner;
public class Grade
{
public static void main(String[] args)
{
char grade;
int value=0;
Scanner s = new Scanner(System.in);
System.out.print("Please enter a grade :");
grade = s.next().charAt(0);
switch(Character.toUpperCase(grade))
{
case 'S':
value= 100;
break;
case 'A':
value=90;
break;
case 'B':
value=80;
break;
case 'C':
value=70;
break;
case 'D':
value=60;
break;
case 'E':
value=50;
break;
default:
System.out.print("The grade provided is incorrect");
}
System.out.print(grade+" = "+value+" ");
}
}
PART C
import java.util.Scanner;
public class Military_name
{
public static void main(String[] args)
{
char letter;
String phonetic="";
Scanner s = new Scanner(System.in);
System.out.print("Please enter a letter: ");
letter = s.next().charAt(0);
switch(Character.toUpperCase(letter))
{
case 'A':
phonetic = "Alpha";
break;
case 'B':
phonetic = "Bravo";
break;
case 'C':
phonetic = "Charlie";
break;
case 'D':
phonetic = "Delta";
break;
case 'E':
phonetic = "Echo";
break;
case 'F':
phonetic = "Foxtrot";
break;
case 'G':
phonetic = "Golf";
break;
default:
System.out.print("Only letters between A and G are acceptable");
}
System.out.print(letter+" = "+phonetic+" ");
}
}