Question

In: Computer Science

CAN YOU CORRECT THIS CODE PLS. Scanner in = new Scanner (System.in);    // EXAMPLE 1...

CAN YOU CORRECT THIS CODE PLS.

Scanner in = new Scanner (System.in);
  
// EXAMPLE 1
System.out.println("****************** EXAMPLE 1 *****************" );
int x = 4, y = 9;
int a = 99, b = -22;

if (x > y)
System.out.println("x > y");
if (x < y)
System.out.println("x < y");
if (x >= y)
System.out.println("x >= y");
if (a <= 10023)
System.out.println("a <= 10023");
if (b == y)
System.out.println("b == y");
if (b == 5)
System.out.println("b == 5");
if (x != 4)
System.out.println("x != 4");
System.out.println();
  
// EXAMPLE 2
System.out.println("****************** EXAMPLE 2 *****************" );
boolean even = false;
x = 6;

if (x % 2 == 0)
even = true;

if (even)
System.out.println("x is even.");
if (even == true)
System.out.println("x is even.");
if (even == false)
System.out.println("x is not even.");

if (true)
System.out.println("This statement WILL output.");
if (false)
System.out.println("This statement will NOT output.");
System.out.println();
  
// EXAMPLE 3
System.out.println("****************** EXAMPLE 3 *****************" );
System.out.print("Select an option:\n" +
" 1. Have Time\n 2. Work Hard\n" +
" 3. Socialize\n\n> ");

int choice = in.nextInt(); // user enters 2

if (choice == 1)
System.out.println("Here's time!");
else if (choice == 2)
System.out.println("Keep it up!");
else if (choice == 3)
System.out.println("Nice weather we're having!");
else
System.out.println("Well, you can't do all three.");
System.out.println();
  
  
// EXAMPLE 4
System.out.println("****************** EXAMPLE 4 *****************" );
int grade = 90;
if (grade >= 90)
System.out.println("You got a A!");
if (grade >= 80)
System.out.println("You got a B!");
if (grade >= 70)
System.out.println("You got a C!");
System.out.println();

// EXAMPLE 5
System.out.println("****************** EXAMPLE 5 *****************" );
a = 4;

if ( a== 4 ) {
System.out.println ( "a is equal to 4!" );
}
System.out.println();

// EXAMPLE 6
System.out.println("******************** EXAMPLE 6 *****************" );
b = 483;

if ( b == 4 )   
System.out.println ( "b is equal to 4!" );
}

// EXAMPLE 7
System.out.println("********************* EXAMPLE 7 *****************" );
a = 483;

if( a == 4 ) {
System.out.println( "a is equal to 4!" );
System.out.println( "That's right -a is equal to 4!" );
if (a == 483 );
System.out.println ( "a is equal to 483!" );
System.out.println ( "That's right -a is equal to 483!" );
System.out.println();
}


// EXAMPLE 8
System.out.println("********************* EXAMPLE 8 *****************" );
b = 483;

if( b == 4 );
{
System.out.println ( "b is equal to 4!" );
}
System.out.println ( "That's right -b is equal to 4!" );
else if ( b == 483 )
{
System.out.println ( "b is equal to 483!" );
}
System.out.println ( "That's right -b is equal to 483!" );
System.out.println();


// EXAMPLE 9
System.out.println("********************* EXAMPLE 9 *****************" );
int number1, number2;
System.out.print ( "Enter first integer: ");
number1 = in.nextInt();
System.out.print ( "Enter second integer: ");
number2 = in.nextInt();

if (number1 == number2)
System.out.println( number1 + " == " + number2 );
if (number1 != number2)
SSystem.out.println( number1 + " != " + number2 );
if (number1 < number2)
System.out.println( number1 + " < " + number2 );
if (number1 > number2)
System.out.println( number1 + " > " + number2 );
if (number1 <= number2)
System.out.println( number1 + " <= " + number2 );
if (number1 >= number2)
System.out.println( number1 + " >= " + number2 );
System.out.println();


// EXAMPLE 10
System.out.println("********************* EXAMPLE 10 ****************" );
grade = 65
String message;
if ( grade >= 60 ) {
message = "You passed";
}
else
message = "You failed";
System.out.println( message );
System.out.println();

}
}

Solutions

Expert Solution

import java.util.Scanner; //added

public class example {
        public static void main(String[] args){ // added main method
                Scanner in = new Scanner (System.in);
          
                // EXAMPLE 1
                System.out.println("****************** EXAMPLE 1 *****************" );
                int x = 4, y = 9;
                int a = 99, b = -22;

                if (x > y)
                        System.out.println("x > y");
                if (x < y)
                        System.out.println("x < y");
                if (x >= y)
                        System.out.println("x >= y");
                if (a <= 10023)
                        System.out.println("a <= 10023");
                if (b == y)
                        System.out.println("b == y");
                if (b == 5)
                        System.out.println("b == 5");
                if (x != 4)
                        System.out.println("x != 4");
                System.out.println();
          
                // EXAMPLE 2
                System.out.println("****************** EXAMPLE 2 *****************" );
                boolean even = false;
                x = 6;

                if (x % 2 == 0)
                        even = true;

                if (even)
                        System.out.println("x is even.");
                if (even == true)
                        System.out.println("x is even.");
                if (even == false)
                        System.out.println("x is not even.");

                if (true)
                        System.out.println("This statement WILL output.");
                if (false)
                        System.out.println("This statement will NOT output.");
                System.out.println();
        
                // EXAMPLE 3
                System.out.println("****************** EXAMPLE 3 *****************" );
                System.out.print("Select an option:\n" +
                                " 1. Have Time\n 2. Work Hard\n" +
                                " 3. Socialize\n\n> ");

                int choice = in.nextInt(); // user enters 2

                if (choice == 1)
                        System.out.println("Here's time!");
                else if (choice == 2)
                        System.out.println("Keep it up!");
                else if (choice == 3)
                        System.out.println("Nice weather we're having!");
                else
                        System.out.println("Well, you can't do all three.");
                System.out.println();
          
          
                // EXAMPLE 4
                System.out.println("****************** EXAMPLE 4 *****************" );
                int grade = 90;
                if (grade >= 90)
                        System.out.println("You got a A!");
                if (grade >= 80)
                        System.out.println("You got a B!");
                if (grade >= 70)
                        System.out.println("You got a C!");
                System.out.println();

                // EXAMPLE 5
                System.out.println("****************** EXAMPLE 5 *****************" );
                a = 4;

                if ( a== 4 ) {
                        System.out.println ( "a is equal to 4!" );
                }
                System.out.println();

                // EXAMPLE 6
                System.out.println("******************** EXAMPLE 6 *****************" );
                b = 483;

                if ( b == 4 )   
                        System.out.println ( "b is equal to 4!" );
                //} removed closing curly bracket... 

                // EXAMPLE 7
                System.out.println("********************* EXAMPLE 7 *****************" );
                a = 483;

                if( a == 4 ) {
                        System.out.println( "a is equal to 4!" );
                        System.out.println( "That's right -a is equal to 4!" );
                }// added closing curly bracket
                if (a == 483 ){//; removed semicolon and added opening curly braces
                        System.out.println ( "a is equal to 483!" );
                        System.out.println ( "That's right -a is equal to 483!" );
                }
                System.out.println();// shifted println to out side the bracket


                // EXAMPLE 8
                System.out.println("********************* EXAMPLE 8 *****************" );
                b = 483;

                if( b == 4 ){//; removed semicolon
                        System.out.println ( "b is equal to 4!" );
                        System.out.println ( "That's right -b is equal to 4!" );// moved into the if condition
                }
                else if ( b == 483 ) {// removed semiclon after else if
                        System.out.println ( "b is equal to 483!" );
                        System.out.println ( "That's right -b is equal to 483!" );// moved into the else if condition
                }
                System.out.println();


                // EXAMPLE 9
                System.out.println("********************* EXAMPLE 9 *****************" );
                int number1, number2;
                System.out.print ( "Enter first integer: ");
                number1 = in.nextInt();
                System.out.print ( "Enter second integer: ");
                number2 = in.nextInt();

                if (number1 == number2)
                        System.out.println( number1 + " == " + number2 );
                if (number1 != number2)
                        System.out.println( number1 + " != " + number2 );//corrected spelling of System
                if (number1 < number2)
                        System.out.println( number1 + " < " + number2 );
                if (number1 > number2)
                        System.out.println( number1 + " > " + number2 );
                if (number1 <= number2)
                        System.out.println( number1 + " <= " + number2 );
                if (number1 >= number2)
                        System.out.println( number1 + " >= " + number2 );
                System.out.println();


                // EXAMPLE 10
                System.out.println("********************* EXAMPLE 10 ****************" );
                grade = 65;// added semicolon
                String message;
                if ( grade >= 60 ) {
                        message = "You passed";
                }
                else 
                        message = "You failed";
                System.out.println( message );
                System.out.println();
        }// removed two closing brackets
}

NOTE : Avoid copy and paste the code directly because class name at the start which is in the code i submitted is "example" it may not be same for you because class name must match to file name .. I will provide screenshot for more clarity

Another thing is careful while using if condition without braces . you must follow proper indentation.

You can copy the code but you must need to change the class name to file name

I'm submitting output that i got below

****************** EXAMPLE 1 *****************
x < y
a <= 10023

****************** EXAMPLE 2 *****************
x is even.
x is even.
This statement WILL output.

****************** EXAMPLE 3 *****************
Select an option:
1. Have Time
2. Work Hard
3. Socialize

> 2
Keep it up!

****************** EXAMPLE 4 *****************
You got a A!
You got a B!
You got a C!

****************** EXAMPLE 5 *****************
a is equal to 4!

******************** EXAMPLE 6 *****************
********************* EXAMPLE 7 *****************
a is equal to 483!
That's right -a is equal to 483!

********************* EXAMPLE 8 *****************
b is equal to 483!
That's right -b is equal to 483!

********************* EXAMPLE 9 *****************
Enter first integer: 9
Enter second integer: 3
9 != 3
9 > 3
9 >= 3

********************* EXAMPLE 10 ****************
You passed

If you are new to use code editors remember Numbers on the left side of images are line numbers they are not part of the code

Hope this will be helpful


Related Solutions

1.The below code has some errors, correct the errors and post the working code. Scanner console...
1.The below code has some errors, correct the errors and post the working code. Scanner console = new Scanner(System.in); System.out.print("Type your name: "); String name = console.nextString(); name = toUpperCase(); System.out.println(name + " has " + name.Length() + " letters"); Sample Ouptut: Type your name: John JOHN has 4 letters    2. Write a code that it reads the user's first and last name (read in the entire line as a single string), then print the last name   followed by...
What is trigger strategy and can you find an example for that? pls go in detail
What is trigger strategy and can you find an example for that? pls go in detail
Java Starter Code: import java.util.Scanner; public class GameScore { static Scanner keyboard = new Scanner(System.in); public...
Java Starter Code: import java.util.Scanner; public class GameScore { static Scanner keyboard = new Scanner(System.in); public static void main(String[] args) { int team1[] = new int[4]; int team2[] = new int[4]; for (int qtr = 0; qtr < 4; qtr++) { quarterScoring(team1, team2, qtr); } int team1Total = teamTotal(team1); int team2Total = teamTotal(team2); displayGameResults(team1, team2); if (team1Total > team2Total) { System.out.println("Team 1 has won the game!"); } else { System.out.println("Team 2 has won the game!"); } } static int teamTotal(int[]...
I was wondering if you can tell me if the following code is correct and if...
I was wondering if you can tell me if the following code is correct and if its not can it be fixed so it does not have any syntax errors. Client one /** * Maintains information on an insurance client. * * @author Doyt Perry/<add your name here> * @version Fall 2019 */ public class Client { // instance variables private String lastName; private String firstName; private int age; private int height; private int weight; /** * First constructor for...
Use psuedo-code for the following: 1) Assign a new student into the correct desk in a...
Use psuedo-code for the following: 1) Assign a new student into the correct desk in a room of students seated in alphabetical order. (Sort by just last name)
You can purchase an optical scanner today for $450. The scanner provides benefits worth $66 a...
You can purchase an optical scanner today for $450. The scanner provides benefits worth $66 a year. The expected life of the scanner is 10 years. Scanners are expected to decrease in price by 20% per year. Suppose the discount rate is 10%. Should you purchase the scanner today? Yes No When is the best purchase time? Wait for 4 years Wait for 5 years Wait for 6 years
You can purchase an optical scanner today for $400. The scanner provides benefits worth $60 a...
You can purchase an optical scanner today for $400. The scanner provides benefits worth $60 a year. The expected life of the scanner is 10 years. Scanners are expected to decrease in price by 20% per year. Suppose the discount rate is 10%. Should you purchase the scanner today or wait to purchase? When is the best purchase time?
You can purchase an optical scanner today for $490. The scanner provides benefits worth $75 a...
You can purchase an optical scanner today for $490. The scanner provides benefits worth $75 a year. The expected life of the scanner is 10 years. Scanners are expected to decrease in price by 25% per year. Suppose the discount rate is 11%. When is the best time to purchase the scanner? (Round your answer to the nearest cent.)                      NPV is maximized when you wait  (Click to select)  0  1  2  3  4  6  5  7  8  9  10  years to purchase the scanner. At this date NPV will be equivalent to...
Instructions: Please make sure everything is correct if not correct and give correct calculation pls. Absorption...
Instructions: Please make sure everything is correct if not correct and give correct calculation pls. Absorption and Variable Costing Income Statements for Two Months and Analysis During the first month of operations ended July 31, Head Gear Inc. manufactured 26,800 hats, of which 25,500 were sold. Operating data for the month are summarized as follows: Sales $183,600 Manufacturing costs: Direct materials $109,880 Direct labor 29,480 Variable manufacturing cost 13,400 Fixed manufacturing cost 10,720 163,480 Selling and administrative expenses: Variable $10,200...
CODE IN JAVA Create a new class named Task1. In its main function, use Scanner to...
CODE IN JAVA Create a new class named Task1. In its main function, use Scanner to read integers from the keyboard with the method nextInt. Use a try catch block to capture non-integer input, and tell the user, "This is not an Integer". The program loop should end when the user enters the string "quit". (Hint: "quit" is clearly not a number and will be captured in the try / catch block.) A typical Output Dialog is shown below: Enter...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT