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...
Can you fix my code and remove the errors in java language. public class LinkedStack<T> implements...
Can you fix my code and remove the errors in java language. public class LinkedStack<T> implements Stack<T> { private Node<T> top; private int numElements = 0; public int size() { return (numElements); } public boolean isEmpty() { return (top == null); } public T top() throws StackException { if (isEmpty()) throw new StackException("Stack is empty."); return top.info; } public T pop() throws StackException { Node<T> temp; if (isEmpty()) throw new StackException("Stack underflow."); temp = top; top = top.getLink(); return temp.getInfo();...
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]++; }   ...
Please Fix Syntax Error import java.util.Scanner; public class SalaryCalc {    double Rpay = 0, Opay...
Please Fix Syntax Error import java.util.Scanner; public class SalaryCalc {    double Rpay = 0, Opay = 0;    void calPay(double hours, double rate) {        if (hours <= 40) {            Rpay = hours * rate;            Opay = 0;        } else {            double Rhr, Ohr;            Rhr = 40;            Ohr = hours - Rhr;            Rpay = Rhr * rate;   ...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT