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...
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...
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...
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...
import java.util.Scanner; public class Months { // You will need to write a method that makes...
import java.util.Scanner; public class Months { // You will need to write a method that makes this // code compile and produce the correct output. // YOU MUST USE switch! // 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 = input.nextInt(); String output = monthAsString(month); System.out.println(output); } }
Please fix all of the errors in this Python Code. import math """ A collection of...
Please fix all of the errors in this Python Code. import math """ A collection of methods for dealing with triangles specified by the length of three sides (a, b, c) If the sides cannot form a triangle,then return None for the value """ ## @TODO - add the errlog method and use wolf fencing to identify the errors in this code def validate_triangle(sides): """ This method should return True if and only if the sides form a valid triangle...
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Exercise { public static void main(String[] args) {...
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Exercise { public static void main(String[] args) { Scanner input=new Scanner(System.in); int[] WordsCharsLetters = {0,1,2}; while(input.hasNext()) { String sentence=input.nextLine(); if(sentence!=null&&sentence.length()>0){ WordsCharsLetters[0] += calculateAndPrintChars(sentence)[0]; WordsCharsLetters[1] += calculateAndPrintChars(sentence)[1]; WordsCharsLetters[2] += calculateAndPrintChars(sentence)[2]; } else break; } input.close(); System.out.println("Words: " + WordsCharsLetters[0]); System.out.println("Characters: " + WordsCharsLetters[1]); System.out.println("Letters: " + WordsCharsLetters[2]); } static int[] calculateAndPrintChars(String sentence) { int[] WCL = new int[3]; String[] sentenceArray=sentence.split(" "); WCL[0] = sentenceArray.length; int letterCount=0; for(int i=0;i<sentence.length();i++) { if(Character.isLetter(sentence.charAt(i))) letterCount++; } WCL[1]...
Can someone fix this code so that it can count comparison and exchange? import java.util.Arrays; class...
Can someone fix this code so that it can count comparison and exchange? import java.util.Arrays; class Main { static int comparisons; static int exchanges; // Recursive function to perform insertion sort on sub-array arr[i..n] public static void insertionSort(int[] arr, int i, int n) { int value = arr[i]; int j = i; // Find index j within the sorted subset arr[0..i-1] // where element arr[i] belongs while (j > 0 && arr[j - 1] > value) { arr[j] = arr[j...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT