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

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

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

  • IT Java code In Lab 8, we are going to re-write Lab 3 and add code...

    IT Java code In Lab 8, we are going to re-write Lab 3 and add code to validate user input. The Body Mass Index (BMI) is a calculation used to categorize whether a person’s weight is at a healthy level for a given height. The formula is as follows:                 bmi = kilograms / (meters2)                 where kilograms = person’s weight in kilograms, meters = person’s height in meters BMI is then categorized as follows: Classification BMI Range Underweight Less...

  • Add on to the java code below to include a welcome message and menu for the...

    Add on to the java code below to include a welcome message and menu for the user. The user should be able to decide when to exit the program ( the user can enter his data included in code below ), and then can either exit, OR enter another one. Need a loop structure in this code. import java.util.Scanner; public class ReduceFraction { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter a numerator: "); int n...

  • Modification of original code so it can include 1) at least one array 2)*New English words...

    Modification of original code so it can include 1) at least one array 2)*New English words that are written/saved to the .txt file(s) must be in alphabetical order* (e.g, Apple cannot be in a lower line than Orange). This is what I'm really struggling with as I don't know how to organize alphabetically only the english word translation without the spanish equivalent word also getting organized. The only idea I have right now is to recreate the code using two...

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