Question

Java programming only Create a Java program that inputs a grade from the user. The grade input from the user will be an...

Java programming only

Create a Java program that inputs a grade from the user. The grade input from the user will be an integer. Once the input is stored, use an if-else-if block of code to determine the letter grade of the inputted integer grade. Do not use a bunch of if statements by themselves to solve this problem. You will print an error message for inputs greater than 100 and for inputs less than 0. Both errors must be handled together by a compound conditional statement that is joined by a short circuit AND or OR operator. Assign your error message to a String variable and use that variable in a System.out.println statement to print the error. You must assign a char variable for each letter grade. Use System.out.printf to print the letter grade.

Run your program several times using these inputs and ensure that you are receiving these outputs:

  • input: 140
    • expected output: ERROR - You have entered an invalid input.
  • Input: -42
    • expected output: ERROR - You have entered an invalid input.
  • input: 85
    • expected output: You have earned the letter grade B.

Run it a few more times using various other inputs.

Part II

Convert the if-else-if code block to a switch statement to solve the problem. Use modulus and/or integer division to convert the grade input so that the range of grades are converted to one value. All other requirements from Part I are still required for part two. Use the same inputs and the outputs should remain the same.

Part III

Write describing why you should not use if statements alone to solve part I and why using if-else-if statements is a better technique. Think about the differences between these two selection structures and why the latter might be preferred for this particular solution.

Part IV

Section 3.15 in your zyBook says, "Strings are considered immutable, meaning they cannot be changed." Do a little research on what this means and on how Strings are handled in memory in Java. Using words and/or a diagram, explain what you've learned and why strings are immutable.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________

// Part 1:

import java.util.Scanner;

public class GradeLetter {

   public static void main(String[] args) {
       int num;
String mesg="";
char gradeLetter = 0;
       /*
       * Creating an Scanner class object which is used to get the inputs
       * entered by the user
       */
       Scanner sc = new Scanner(System.in);
       while(true)
       {
           System.out.print("input: ");
           num=sc.nextInt();
          
           if(num<0 || num>100)
           {
               mesg="ERROR - You have entered an invalid input.";
              
           }
           else
           {
               if (num >= 90)
                   gradeLetter = 'A';
                   else if (num >= 80 && num < 90)
                   gradeLetter = 'B';
                   else if (num >= 70 && num < 80)
                   gradeLetter = 'C';
                   else if (num >= 60 && num < 70)
                   gradeLetter = 'D';
                   else if (num < 60)
                   gradeLetter = 'F';
              
               break;
           }  
           System.out.println(mesg);
       }
      
       System.out.printf("You have earned the letter grade %c.",gradeLetter);
   }

}

_________________________

output:

input: 140
ERROR - You have entered an invalid input.
input: -42
ERROR - You have entered an invalid input.
input: 85
You have earned the letter grade B.

__________________________

2)

// using Switch

import java.util.Scanner;

public class GradeLetterSwitch {

   public static void main(String[] args) {
       int num;
       String mesg = "";
       char gradeLetter = 0;
       /*
       * Creating an Scanner class object which is used to get the inputs
       * entered by the user
       */
       Scanner sc = new Scanner(System.in);
       while (true) {
           System.out.print("input: ");
           num = sc.nextInt();

           if (num < 0 || num > 100) {
               mesg = "ERROR - You have entered an invalid input.";

           } else {
               num /= 10;
               switch (num) {
               case 10:
               case 9: {
                   gradeLetter = 'A';
                   break;
               }
               case 8: {
                   gradeLetter = 'B';
                   break;
               }
               case 7: {
                   gradeLetter = 'C';
                   break;
               }
               case 6: {
                   gradeLetter = 'D';
                   break;
               }
               case 5: {
                   gradeLetter = 'F';
                   break;
               }

               }

               break;
           }
           System.out.println(mesg);
       }

       System.out.printf("You have earned the letter grade %c.", gradeLetter);

   }

}

_________________________

output:

input: 140
ERROR - You have entered an invalid input.
input: -42
ERROR - You have entered an invalid input.
input: 85
You have earned the letter grade B.

__________________________

3)

If we are using multiple IF statements , if we want to use a condition like if(num<100).Then for the numbers like 95 or 79 or 65 ,the if condition will get executed like that there can be a chance that more that one if condition is executed.The we may get incorrect output.

But if we use If-Else-If block only one condition will be executed.Then we may get the exact output.

__________________________

4)

4) String objects are Immutable, meaning they can’t grow or shrink.

Immutable:

We can modify the content of the String object.

i.e if we have a string object which contains some content inside it.If we want to append some date to that existing string content.Then it is not possible to append.

If we try to append by using concat operator(+),then a new String object is created with that new content.Previous reference variable stops referencing the old String Object and start referencing the newly created String object.

String str1=”hi”;//A string object contains the content “hi” which is referenced by str1 reference variable.

System.out.println(st1+” Kane”);//Here a new String object is created which contains the content “hi Kane” which is pointed by the reference variable str1.The previous string object don’t have any reference variable now.Which will be removed by the garbage collector.

_______________Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
Java programming only Create a Java program that inputs a grade from the user. The grade input from the user will be an...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Part I Create a Java program that inputs a grade from the user. The grade input...

    Part I Create a Java program that inputs a grade from the user. The grade input from the user will be an integer. Once the input is stored, use an if-else-if block of code to determine the letter grade of the inputted integer grade. Do not use a bunch of if statements by themselves to solve this problem. You will print an error message for inputs greater than 100 and for inputs less than 0. Both errors must be handled...

  • Below is a java program that inputs an integer grade and turns it into a letter...

    Below is a java program that inputs an integer grade and turns it into a letter grade. Update the below java code as follows and comment each line to explain what is happening: 1. Convert the if-else-if code block to a switch statement to solve the problem. 2. Use modulus to convert the grade input so that the range of grades are converted to one value. (comment the line) import java.util.Scanner; public class GradeLetterTest { public static void main(String[] args)...

  • This C# program prints out a corresponding letter grade for a given mark. It uses a...

    This C# program prints out a corresponding letter grade for a given mark. It uses a method (called MarktoGrade) to determine the letter grade. MarktoGrade takes one integer call by value formal parameter (called mark) and returns a char value that is the letter grade. All of the input and output (except an error message) is done in Main. There is one syntax error in this program that you will need to fix before it will compile (the error is...

  • hello. i need help with number 2 ONLY 1. Use if statements to write a Java...

    hello. i need help with number 2 ONLY 1. Use if statements to write a Java program that inputs a single letter and prints out the corresponding digit on the telephone. The letters and digits on a telephone are grouped this way 2=ABC 3 = DEF 4 GHI 5 JKL 6 - MNO 7 - PRS 8 - TUV 9-WXY No digit corresponds to either Qor Z. For these 2 letters your program should print a message indicating that they...

  • Java Programming Language Edit and modify from the given code Perform the exact same logic, except...

    Java Programming Language Edit and modify from the given code Perform the exact same logic, except . . . The numeric ranges and corresponding letter grade will be stored in a file. Need error checking for: Being able to open the text file. The data makes sense: 1 text line of data would be 100 = A. Read in all of the numeric grades and letter grades and stored them into an array or arraylist. Then do the same logic....

  • Write a Java program that inputs a list of integer values in the range of −...

    Write a Java program that inputs a list of integer values in the range of − 1 to 100 from the keyboard and computes the sum of the squares of the input values. This program must use exception handling to ensure that the input values are in range and are legal integers, to handle the error of the sum of the squares becoming larger than a standard Integer variable can store, and to detect end-of-file and use it to cause...

  • Matlab a) Write a MATLAB code that takes the grade letters from the user and provide him/her with the GPA for that seme...

    Matlab a) Write a MATLAB code that takes the grade letters from the user and provide him/her with the GPA for that semester Enter your grades (letters): AABBC (input) Your GPA is 3.2 output) b) Write a Matlab user defined function to that takes a letter grade and return a numeric grade as shown in the table below. Any other value should give an error message (Invalid Grade). You function name should be Letter2Grade Use the following information to calculate...

  • Need Java help: 1. Create a Java program that accepts input String input from a user...

    Need Java help: 1. Create a Java program that accepts input String input from a user of first name. 2. Create a Java program that accepts input String input from a user of last name. 3. Concatenate the Strings in a full name variable and print to the console.

  • Write a C++ program that will provide a user with a grade range if they enter...

    Write a C++ program that will provide a user with a grade range if they enter a letter grade. Your program should contain one function. Your function will accept one argument of type char and will not return anything (the return type will be void), but rather print statements to the console. Your main function will contain code to prompt the user to enter a letter grade. It will pass the letter grade entered by the user to your function....

  • Question 1: Consider the following specification for a program. The program reads two inputs from the...

    Question 1: Consider the following specification for a program. The program reads two inputs from the user: a string s and an integer number n. The program concatenates s with itself n-1 times, if the following conditions are satisfied: 1- s consists of 3 characters. 2- The first character in s is a letter. 3- s consists of letters (A-Z a-z) and digits only (0-9) 4-0< n<5 If any of these conditions is not satisfied, the program terminates and prints...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT