Question

In: Computer Science

Can you fix the errors in this code? import java.util.Scanner; public class Errors6 {    public...

Can you fix the errors in this code?

import java.util.Scanner;

public class Errors6 {

   public static void main(String[] args) {
       System.out.println("This program will ask the user for three sets of two numbers and will calculate the average of each set.");

       Scanner input = new Scanner(System.in);

       int n1, n2;

       System.out.print("Please enter the first number: ");
       n1 = input.nextInt();

       System.out.print("Please enter the second number: ");
       n2 = input.nextInt();

       int average;
       average = (n1+n2)/2;
       System.out.print("The average of the numbers is " + average);

       Scanner input = new Scanner(System.in);

       float n1, n2;

       System.out.print("Please enter the first number:");
       n1 = input.nextFloat();

       System.out.print("Please enter the second number: ");
       n2 = input.nextFloat();

       float average;
       average = (n1+n2)/2;
       System.out.print("The average of the numbers is " + average);

       short s1, s2;

       System.out.print("Please enter the first number: ");
       s1 = input.nextShort();

       System.out.print("Please enter the second number: ");
       s2 = input.nextShort();

       short shortAvg;
       shortAvg = (short)(n1+n2)/2;
       System.out.print("The average of the numbers is " + shortAvg);
   }

}

Solutions

Expert Solution

Program:

Explanation:

Line 1: //Header file for scanner object
Line 2: //Here taking class name as Errors6
Line 3: //Start of main()
Line 4: //Printing the statement
Line 5:// Here creating the scanner object called input
Line 6://Here decalring the integer varaibles called n1 & n2
Line 7://Here asking the user to enter first number
Line 8://Here scanning the user input for n1
Line 9://Here asking the user to enter second number
Line 10://Here scanning the user input for n2
Line 11://Here decalring the integer varaibles called average
Line 12://Calculating the average value
Line 13://Here printing the value of average
Line 14://Here decalring the float varaibles called n3 & n4
Line 15://Here asking the user to enter first number
Line 16://Here scanning the user input for n3
Line 17://Here asking the user to enter second number
Line 18://Here scanning the user input for n4
Line 19://Here decalring the float varaibles called FloatAvg
Line 20://Calculating the FloatAvg value
Line 21://Here printing the value of FloatAvg
Line 22://Here decalring the short varaibles called s1 & s2
Line 23://Here asking the user to enter first number
Line 24://Here scanning the user input for s1
Line 25://Here asking the user to enter second number
Line 26://Here scanning the user input for s2
Line 27://Here decalring the short varaibles called shortAvg
Line 28://Calculating the shortAvg value
Line 29://Here printing the value of shortAvg
Line 35: //End of main()
Line 36://End of class Errors6

Program:

import java.util.Scanner;

class Errors6 {
public static void main(String[] args) {
System.out.println("This program will ask the user for three sets of two numbers and will calculate the average of each set.");
Scanner input = new Scanner(System.in);
int n1, n2;
System.out.print("Please enter the first number: ");
n1 = input.nextInt();
System.out.print("Please enter the second number: ");
n2 = input.nextInt();
int average;
average = (n1+n2)/2;
System.out.print("The average of the numbers is " + average);
float n3, n4;
System.out.print("Please enter the first number:");
n3 = input.nextFloat();
System.out.print("Please enter the second number: ");
n4 = input.nextFloat();
float FloatAvg;
FloatAvg = (n3+n4)/2;
System.out.print("The average of the numbers is " + FloatAvg);
short s1, s2;
System.out.print("Please enter the first number: ");
s1 = input.nextShort();
System.out.print("Please enter the second number: ");
s2 = input.nextShort();
short shortAvg;
shortAvg = (short)((s1+s2)/2);
System.out.print("The average of the numbers is " + shortAvg);
}
}

Output:


Related Solutions

Can you fix the errors in this code? package demo; /** * * */ import java.util.Scanner;...
Can you fix the errors in this code? package demo; /** * * */ import java.util.Scanner; public class Booolean0p {        public class BooleanOp {            public static void main(String[] args) {                int a = 0, b = 0 , c = 0;                Scanner kbd = new Scanner(System.in);                System.out.print("Input the first number: ");                a = kbd.nextInt();                System.out.print("Input...
----fix code to search and delete a student by Identification number import java.util.Scanner; public class COurseCom666...
----fix code to search and delete a student by Identification number import java.util.Scanner; public class COurseCom666 {     private String courseName;     private String[] students = new String[1];     private int numberOfStudents;     public COurseCom666(String courseName) {         this.courseName = courseName;     }     public String[] getStudents() {         return students;     }     public int getNumberOfStudents() {         return numberOfStudents;     }     public String getCourseName() {         return courseName;     }     public int DeleteStudentsByID() {         return...
What is wrong with this code and how can it be fixed? import java.util.Scanner; public class...
What is wrong with this code and how can it be fixed? import java.util.Scanner; public class admissionRequirement { public static void main(String[] args) { // TODO Auto-generated method stub Scanner myObj = new Scanner(System.in); System.out.println("What is your name?"); String name = myObj.nextLine(); System.out.println("What is your Reading Score?"); int reading = myObj.nextInt(); System.out.println("What is your Math Score?"); int math = myObj.nextInt(); System.out.println("What is your Writing Score?"); int writing = myObj.nextInt(); System.out.println("What is your Class Standing?"); int standing = myObj.nextInt(); System.out.println("What is...
Can you fix my code and remove the errors? Thank you!! ^^ ////////////////////////////////////////////////////////////////////////////////////////////////////// public class StackException<T,...
Can you fix my code and remove the errors? Thank you!! ^^ ////////////////////////////////////////////////////////////////////////////////////////////////////// public class StackException<T, size> extends Throwable { private final T[] S = null ; public StackException(String s) { } public T top() throws StackException { if (isEmpty()) throw new StackException("Stack is empty."); int top = 0; return S[top]; } private boolean isEmpty() { return false; } public T pop() throws StackException { T item; if (isEmpty()) throw new StackException("Stack underflow."); int top = 0; item = S[top];...
TASK: Based upon the following code: import java.util.Scanner; // Import the Scanner class public class Main...
TASK: Based upon the following code: import java.util.Scanner; // Import the Scanner class public class Main {   public static void main( String[] args ) {     Scanner myInput = new Scanner(System.in); // Create a Scanner object     System.out.println("Enter (3) digits: ");     int W = myInput.nextInt();     int X = myInput.nextInt();     int Y = myInput.nextInt();      } } Use the tools described thus far to create additional code that will sort the integers in either monotonic ascending or descending order. Copy your code and...
My code: import java.util.Random; import java.util.Scanner; public class RollDice { public static void main(String[] args) {...
My code: import java.util.Random; import java.util.Scanner; public class RollDice { public static void main(String[] args) { int N; Scanner keybd = new Scanner(System.in); int[] counts = new int[12];    System.out.print("Enter the number of trials: "); N = keybd.nextInt();    Random die1 = new Random(); Random die2 = new Random(); int value1, value2, sum; for(int i = 1; i <= N; i++) { value1 = die1.nextInt(6) + 1; value2 = die2.nextInt(6) + 1; sum = value1 + value2; counts[sum-1]++; }   ...
import java.util.Random; import java.util.Scanner; public class Compass { // You will need to do the following:...
import java.util.Random; import java.util.Scanner; public class Compass { // You will need to do the following: // // 1.) Define a private instance variable which can // hold a reference to a Random object. // // 2.) Define a constructor which takes a seed value. // This seed will be used to initialize the // aforementioned Random instance variable. // // 3.) Define a static method named numberToDirection // which takes a direction number and returns a String // representing...
submit the following files: Main.java Consider the following code: import java.util.Scanner; public class Main { public...
submit the following files: Main.java Consider the following code: import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String input = ""; System.out.println("Enter the first number: "); input = in.nextLine(); int a = 0; int b = 0; a = Integer.parseInt(input); System.out.println("Enter the second number: "); input = in.nextLine(); b = Integer.parseInt(input); int result = 0; result = a/b; System.out.println("a/b : " + result); } Copy the code to your Main.java file...
Correct the code: import java.util.Scanner; public class Ch7_PrExercise5 { public static void main(String[] args) {   ...
Correct the code: import java.util.Scanner; public class Ch7_PrExercise5 { public static void main(String[] args) {    Scanner console = new Scanner(System.in);    double radius; double height; System.out.println("This program can calculate "+ "the area of a rectangle, the area "+ "of a circle, or volume of a cylinder."); System.out.println("To run the program enter: "); System.out.println("1: To find the area of rectangle."); System.out.println("2: To find the area of a circle."); System.out.println("3: To find the volume of a cylinder."); System.out.println("-1: To terminate the...
import java.util.Scanner; public class MonthsOnAndAfter { // You will need to write a method that makes...
import java.util.Scanner; public class MonthsOnAndAfter { // You will need to write a method that makes this // code compile and produce the correct output. // YOU MUST USE switch! // As a hint, you should not have to use the name of each // month more than once. // TODO - write your code below this comment. // DO NOT MODIFY main! public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter month (0-11): "); int month...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT