MAKE SURE YOU USE CONDITIONALS FOR 1.A AND WHILE LOOPS FOR 1.B. IF YOU DON\'T, I
ID: 3601955 • Letter: M
Question
MAKE SURE YOU USE CONDITIONALS FOR 1.A AND WHILE LOOPS FOR 1.B. IF YOU DON'T, IT WILL BE FLAGGED FOR REVIEW.
MAKE SURE YOU ANSWER PART 1.A AND 1.B CORRECTLY. OR ELSE IT WILL BE FLAGGED.
Make sure you input comments and formatting, please. These are must Use conditionals for 1.A and while loops for 1.B, please. These are must Answer both 1 . A and 1. B and incorrect answers or incomplete work will be flagg 1A. for a review. Write a Java program that prompts the user to enter in two countries. The program then prints out which country has more letters or if they have the same number of letters, the first character of both countries, and if the countries entered are the same. Test the program with two sets of data (one showing the cities have different number of characters and one showing that they have the same number of letters) The program works as follows: Enter two cities: Halifax Bedford Halifax and Bedford have the same number of letters The first character of Halifax is H. The first character of Bedford is B Halifax and Bedford are not the same city. 1.B Write a complete program that will use a Scanner to ask for the total marks for your tests, assignments and labs each as a percentage (out of 100). If the lab mark is higher than the assignments the lab mark is omitted and the tests are weighted at 60% and the assignments at 40%. Otherwise the tests are weighted at 50% the assignments at 40% and the labs at 10%. The program then computes and prints your final grade as a letter. If you get 80% or higher the letter grade is an A, 70 or higher is a B, 60 or higher a C, 50 or higher a D and less than 50 a F. Test the program with two sets of data and include the results in this report that show both outcomes for the final grade calculation (with two different letter grades).Explanation / Answer
import java.util.Scanner;
// Class Check definition
public class Check
{
// Method to check the city name
public static void checkCity()
{
// Scanner class object created to accept data
Scanner sc = new Scanner(System.in);
// Declares two strings to store city name
String cityOne, cityTwo;
// Accepts city name
System.out.print("Enter two cities: ");
cityOne = sc.next();
cityTwo = sc.next();
// Checks if both the city name number of letters are same
if(cityOne.length() == cityTwo.length())
{
// Displays the message
System.out.println(cityOne + " and " + cityTwo + " have same number of letters");
// Displays each city first letter
System.out.print("The first character of " + cityOne + " is " + cityOne.charAt(0) + ".");
System.out.println("The first character of " + cityTwo + " is " + cityTwo.charAt(0));
// Checks if both the city names are same or not
if(cityOne.compareTo(cityTwo) == 0)
System.out.println(cityOne + " and " + cityTwo + " are the same city");
else
System.out.println(cityOne + " and " + cityTwo + " are not the same city");
}// End of if
// Otherwise checks if first city number of characters is greater than the second city
// Then display message first city has more number of letters
else if(cityOne.length() > cityTwo.length())
System.out.println(cityOne + " has more number of letters then " + cityTwo);
// Otherwise second city number of characters is greater than the first city
// Then display message second city has more number of letters
else
System.out.println(cityTwo + " has more number of letters then " + cityOne);
}// End of method
//main method definition
public static void main(String[] args)
{
// Calls the method twice
checkCity();
checkCity();
}// End of main method
}// End of class
Sample Run:
Enter two cities: Berhampur Baripada
Berhampur has more number of letters then Baripada
Enter two cities: Berhampur Bangalore
Berhampur and Bangalore have same number of letters
The first character of Berhampur is B.The first character of Bangalore is B
Berhampur and Bangalore are not the same city
----------------------------------------------------------------------------------------------------------------------------
import java.util.Scanner;
// Class GradeCalculation definition
public class GradeCalculation
{
// Instance variables to store marks
double tests;
double assignments;
double labs;
double total;
char grade;
// Scanner class object created to accept data
static Scanner sc = new Scanner(System.in);
// Method to accept mark and validate mark
void acceptMark()
{
// Loops till valid test percentage mark entered by the user
do
{
// Accepts mark
System.out.println("Enter test percentage (out of 100): ");
tests = sc.nextDouble();
// Checks if the mark is greater than or equals to zero and less than or equals to 100
// then it is valid mark, come out of the loop
if(tests >=0 && tests <= 100)
break;
}while(true);
// Loops till valid assignment percentage mark entered by the user
do
{
// Accepts mark
System.out.println("Enter assignments percentage (out of 100): ");
assignments = sc.nextDouble();
// Checks if the mark is greater than or equals to zero and less than or equals to 100
// then it is valid mark, come out of the loop
if(assignments >=0 && assignments <= 100)
break;
}while(true);
// Loops till valid lab percentage mark entered by the user
do
{
// Accepts mark
System.out.println("Enter labs percentage (out of 100): ");
labs = sc.nextDouble();
// Checks if the mark is greater than or equals to zero and less than or equals to 100
// then it is valid mark, come out of the loop
if(labs >=0 && labs <= 100)
break;
}while(true);
}// End of method
// Method to calculate and display grade
void calculateGrade()
{
// Checks if lab mark is greater than assignment mark ignore lab mark
if(labs > assignments)
// Calculates total percentage without lab mark
total = tests * .6 + assignments * .4 ;
// Otherwise
else
// Calculates total percentage with lab mark
total = tests * .5 + assignments * .4 + labs * .1;
// Checks if total percentage is greater then or equals to 80
if(total >= 80)
// Assign letter 'A' to grade
grade = 'A';
// Otherwise checks if total percentage is greater then or equals to 70
else if(total >= 70)
// Assign letter 'B' to grade
grade = 'B';
// Otherwise checks if total percentage is greater then or equals to 60
else if(total >= 60)
// Assign letter 'C' to grade
grade = 'C';
// Otherwise checks if total percentage is greater then or equals to 50
else if(total >= 50)
// Assign letter 'D' to grade
grade = 'D';
// Otherwise less than 50
else
// Assign letter 'F' to grade
grade = 'F';
System.out.println("Student got " + grade + " grade");
}// End of method
// main method definition
public static void main(String[] args)
{
// Local variable to store number of students entered by the user
int no;
// Accepts number of students from the user
System.out.println("Enter how many students: ");
no = sc.nextInt();
// Creates an array of object for class GradeCalculation
GradeCalculation []gc = new GradeCalculation[no];
// Loops till number of students
for(int x = 0; x < no; x++)
{
// Instantiate class object
gc[x] = new GradeCalculation();
// Calls the method to accept mark for each student
gc[x].acceptMark();
}// End of for loop
// Loops till number of students
for(int x = 0; x < no; x++)
// Calls the method to display grade of each student
gc[x].calculateGrade();
}// End of main method
}// End of class
Sample Run:
Enter how many students:
3
Enter test percentage (out of 100):
45
Enter assignments percentage (out of 100):
42
Enter labs percentage (out of 100):
55
Enter test percentage (out of 100):
89
Enter assignments percentage (out of 100):
88
Enter labs percentage (out of 100):
70
Enter test percentage (out of 100):
77
Enter assignments percentage (out of 100):
66
Enter labs percentage (out of 100):
99
Student got F grade
Student got A grade
Student got B grade
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.