1. Write a program segment that translates an integer value into a letter grade.
ID: 3784554 • Letter: 1
Question
1. Write a program segment that translates an integer value into a letter grade. Between 90 and 100 the segment should output 'A', between 80 and 89 it should output 'B', between 70 and 79 it should output 'C', between 60 and 69 it should output 'D' and less than 60 it should output 'E'. The last line of your program segment should be a println that outputs the letter grade. Your code should use an int named numberGrade for comparisons.
2. Write a program segment that decides whether a given year is a leap year or not. A year is a leap year if it is divisible by 4, except for the case when it is also divisible by 100 and not by 400 hundred. So, for example, 1999 is not a leap year because it is not divisible by 4; 2000 is a leap year because it is divisible by 4 and although it is divisible by 100, it is also divisible by 400; 1900 however, is not a leap year because it is divisible by 100 and not by 400. Finally, 2004 is a leap year because it is divisible by 4 and not by 100. Your code should use an int named year for comparisons and print "LEAP YEAR" if the value in year is a leap year and "NOT A LEAP YEAR" if it is not. HINT: You will need to use nested if statements in this code!
3. There is something wrong with the following program segment. Describe in English what is wrong with it and what the behavior of this code will be.
int i=0, total=0;
while ( i < 10 ) {
total = total + i;
i = i - 1; }
}
Explanation / Answer
1)
package org.students;
import java.util.Scanner;
public class FindGradeLetter {
public static void main(String[] args) {
//Declaring variables
int numberGrade;
char grade = 0;
//Scanner object is used to get the inputs entered by the user
Scanner sc=new Scanner(System.in);
System.out.print("Enter the Number Grade :");
numberGrade=sc.nextInt();
/* based on the number grade range
* the corresponding grade letter will be displayed
*/
if(numberGrade>=90 && numberGrade<=100)
grade='A';
else if(numberGrade>=80 && numberGrade<=89)
grade='B';
else if(numberGrade>=70 && numberGrade<=79)
grade='C';
else if(numberGrade>=60 && numberGrade<=69)
grade='D';
else if(numberGrade<60)
grade='E';
System.out.println("The Letter Grade is "+grade);
}
}
____________________
output:
Enter the Number Grade :78
The Letter Grade is C
____________________
2)
package org.students;
import java.util.Scanner;
public class CheckLeapYear {
public static void main(String[] args) {
/*
* Declaring and initializing a variable
*/
int year = 0;
/*
* Scanner object is used to read the values entered by the user.
*/
Scanner sc = new Scanner(System.in);
//Getting the year entered by the user
System.out.print("Enter a year :");
year = sc.nextInt();
// Checking whether the year divisible by 4 or not.
if (year % 4 != 0) {
System.out.println("NOT A LEAP YEAR");
}
// Checking whether the year divisible by 100 and the year not
// divisible by 400.
else if (year % 100 == 0 && year % 400 != 0) {
System.out.println("NOT A LEAP YEAR");
}
// Checking whether the year divisible by 100 and the year divisible
// by 400.
else if (year % 100 == 0 && year % 400 == 0) {
System.out.println("LEAP YEAR");
}
// Checking whether the year is not divisible by 100 and the year
// divisible by 4.
else if (year % 100 != 0 && year % 4 == 0) {
System.out.println("LEAP YEAR");
}
}
}
_________________________
output:
Enter a year :2000
LEAP YEAR
________________________
output1:
Enter a year :1900
NOT A LEAP YEAR
________________________
Output2:
2Enter a year :004
LEAP YEAR
________________________
3)
The actual intension of this piece of code is to find the sum of number from 1 to 9
but we provided i=i-1; this statement is wrong because it add all negative numbers and that to this will
leads to the infinite loop as the condition i<10 is always true.
So we have to do some changes in this code.
The actual code must be:
int i=0, total=0;
while ( i < 10 )
{
total = total + i;
i = i + 1;
}
Here this piece of code will find the sum of numbers from 1 to 9 which is 45.
__________Thank you
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.