Question

Your code should contain appropriate validations and must focus on code optimization. Briefly, explain (150-200 words)...

Your code should contain appropriate validations and must focus on code optimization. Briefly, explain (150-200 words) the logic of the program and how the code can be   optimized.

Modify the code to repeat the program until the user enters “no”.

(*I need the logical write out on how the program behaves when there is an input from the user. (I don't need the //comments).

import java.util.Scanner;

public class Q2

{

    public static void main(String[] args)

    {

        Scanner kb = new Scanner(System.in);

        int studentNo, score;

        String Yes;

        do

        {

            System.out.println("Enter Your ID Number : ");

            studentNo = kb.nextInt();

            System.out.println("Enter Your Marks : ");

            score = kb.nextInt();

            if (score >= 85 && score <= 100)

            {

                System.out.println("Your Grade is HD");

            }

            else if (score >= 75 && score <= 84)

            {

                System.out.println("Your Grade is D");

            }

            else if (score >= 65 && score <= 74)

            {

                System.out.println("Your Grade is C");

            }

            else if (score >= 50 && score <= 64)

            {

                System.out.println("Your Grade is P");

            }

            else if (score >=0 && score <50)

            {

                System.out.println("Your Grade is F");

            }

            else

            {

                    System.out.println("Invalid Marks");

                    }

            System.out.println("Do You want to continue (Yes/No): ");

            Yes = kb.next();           

        }

        while(Yes.equalsIgnoreCase("Yes"));

       

    }

   

}

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

`Hey,

Note: Brother in case of any queries, just comment in box I would be very happy to assist all your queries

The program basically repeats itself until user enters anything other than yes. In that loop it asks user for ID of student and its marks. Then it checks in which range does the marks fall. Marks should be in range 0 and 100 if anything other is entered it outputs invalid marks. If the marks are in range then it outputs the corresponding grade. then in the loop it asks user if he wants to continue (yes/no) and checks if user entered something other then yes case insensitive then it breaks.

import java.util.Scanner;

public class Q2

{

    public static void main(String[] args)

    {

        Scanner kb = new Scanner(System.in);

        int studentNo, score;

        String Yes;

        do

        {

            System.out.println("Enter Your ID Number : ");

            studentNo = kb.nextInt();

            System.out.println("Enter Your Marks : ");

            score = kb.nextInt();

            if (score >= 85 && score <= 100)

            {

                System.out.println("Your Grade is HD");

            }

            else if (score >= 75 && score <= 84)

            {

                System.out.println("Your Grade is D");

            }

            else if (score >= 65 && score <= 74)

            {

                System.out.println("Your Grade is C");

            }

            else if (score >= 50 && score <= 64)

            {

                System.out.println("Your Grade is P");

            }

            else if (score >=0 && score <50)

            {

                System.out.println("Your Grade is F");

            }

            else

            {

                    System.out.println("Invalid Marks");

                    }

            System.out.println("Do You want to continue (Yes/No): ");

            Yes = kb.next();           

        }

        while(!Yes.equalsIgnoreCase("No"));

       

    }

   

}

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
Your code should contain appropriate validations and must focus on code optimization. Briefly, explain (150-200 words)...
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
  • Identify a logical error in the following code and fix it. public class test1 {    ...

    Identify a logical error in the following code and fix it. public class test1 {     public static void main(String[] args){         int total;         float avg;         int a, b, c;         a=10;         b=5;         c=2;         total=a+b+c;         avg=total/3;         System.out.println("Average: "+avg);     } } Answer: Write the output of the program below. import java.util.Scanner; public class test2 { public static void main(String[] arg){ int psid; String name; Scanner input=new Scanner(System.in); System.out.println("Enter your PSID"); psid=input.nextInt(); System.out.println("Enter your...

  • please help me with this problem The following program uses if-else-if statements to determine a letter...

    please help me with this problem The following program uses if-else-if statements to determine a letter grade. Rewrite it using if statements with logical operators (&&, ||, !) to produce the same output when given the same inputs. import java.util.Scanner; public class GradeCalculator{ public static void main(String[] args){ int grade; Scanner input = new Scanner(System.in); System.out.println("Enter numeric grade out of 100: "); grade = input.nextInt(); if (grade >= 90){ System.out.println("Congratulations! You got an A!"); } else if (grade >= 80){...

  • Write a JAVA program that prompts the user for student grades and displays the highest and...

    Write a JAVA program that prompts the user for student grades and displays the highest and lowest grades in the class. The user should enter a character to stop providing values. Starter code: import java.util.Scanner; public class MaxMinGrades{   public static void main(String[] args){     Scanner input = new Scanner(System.in);     System.out.println("Enter as many student grades as you like. Enter a character to stop.");     double grade = input.nextDouble();     double minGrade = Double.MAX_VALUE;     double maxGrade = Double.MIN_VALUE;     while (Character.isDigit(grade)) {       if (grade == 0)...

  • The files provided in the code editor to the right contain syntax and/or logic errors. In...

    The files provided in the code editor to the right contain syntax and/or logic errors. In each case, determine and fix the problem, remove all syntax and coding errors, and run the program to ensure it works properly. // DebugFive2.java // Decides if two numbers are evenly divisible import java.util.Scanner; public class DebugFive2 { public static void main(String args[]) { int num; int num2; Scanner input = new Scanner(System.in); System.out.print("Enter a number "); num = input.nextInteger() System.out.print("Enter another number ");...

  • CAN YOU PLEASE DO THIS IN JAVA! WILL LEAVE GOOD RATING THANK YOU Modify your program...

    CAN YOU PLEASE DO THIS IN JAVA! WILL LEAVE GOOD RATING THANK YOU Modify your program that reads grades from the user, so that it has a method that checks if a particular input is valid i.e. as long as the user types invalid input, the user should be given another chance to enter input (it would also be good to let the user know that their input is invalid). Moreover, only a valid grade should be used in computing...

  • JAVA: Write a program that prints a rectangle with a border of asterisks (*). Prompt the...

    JAVA: Write a program that prints a rectangle with a border of asterisks (*). Prompt the user for the width and height of the rectangle. For example: Enter the width and the height of our box. 3 5 *** * * * * * * *** import java.util.Scanner; public class DrawBox { public static void main(String[] args) { Scanner in = new Scanner(System.in);        System.out.println("Enter the width and the height of our box."); /* Type your code here. */...

  • read the code and comments, and fix the program by INSERTING the missing code in Java...

    read the code and comments, and fix the program by INSERTING the missing code in Java THank you please import java.util.Scanner; public class ExtractNames { // Extract (and print to standard output) the first and last names in "Last, First" (read from standard input). public static void main(String[] args) { // Set up a Scanner object for reading input from the user (keyboard). Scanner scan = new Scanner (System.in); // Read a full name from the user as "Last, First"....

  • 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)...

  • Step 4: Add code that discards any extra entries at the propmt that asks if you...

    Step 4: Add code that discards any extra entries at the propmt that asks if you want to enter another score. Notes from professor: For Step 4, add a loop that will validate the response for the prompt question:       "Enter another test score? (y/n): " This loop must only accept the single letters of ‘y’ or ‘n’ (upper case is okay). I suggest that you put this loop inside the loop that already determines if the program should collect...

  • Java debugging in eclipse package edu.ilstu; import java.util.Scanner; /** * The following class has four independent...

    Java debugging in eclipse package edu.ilstu; import java.util.Scanner; /** * The following class has four independent debugging * problems. Solve one at a time, uncommenting the next * one only after the previous problem is working correctly. */ public class FindTheErrors { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); /* * Problem 1 Debugging * * This problem is to read in your first name, * last name, and current year and display them in *...

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