I am doing a homework assignment. I am confused as to whether or not the stored variables will be saved, so do I create a new line for every item that I will need to recall later? that is what I have done, but do I need too? I have pasted what code have wriiten so far.(below)
Here is what it asks for:
Create a
Java program called AverageWeight.java that uses the Scanner class to store the weights, expressed in pounds, of five people in appropriately named variables. The program should calculate the average of these weights and express the average in kilograms (1 pound = .454 kilograms.) The conversion factor should be stored in a named constant. Express the average to the nearest tenth of a kilogram. (Hint:To round a number to the nearest 0.1, add half that, 0.05, to the number, multiply it by 10, convert it to an integer, then divide it by 10.0. For example, 65.78 + .05 becomes 65.83, 65.83 times 10 is 658.3, then 658.3 converted to an integer is 658, and finally 658 divided by 10.0 is 65.8, which is 65.78 rounded to the nearest 0.1.) Output all the original and new information in the command window.
import java.util.Scanner;
public class AverageWeight
{
// main method
public static void main(
String[] args )
{
//declare and initialize variables
String person1; person2; person3; person4; person5; allPersons, outputMessage;
int weight1, weight2, weight3, weight4, weight5, allWeights, totalWeight, averageWeight, KILO_COVERSION, kiloWeight;
final double KILO_CONVERSION = .454; // a named constant
//get required input
Scanner input = new Scanner( System.in );
System.out.print("Enter your name: " );
person1 = input.nextLine();
System.out.print("Person1 enter your weight in pounds: " );
weight1 = input.nextLine();
System.out.print("Enter your name: " );
person2 = input.nextLine();
System.out.print("Person2 enter your weight in pounds: " );
weight2 = input.nextLine();
System.out.print("Enter your name: " );
person3 = input.nextLine();
System.out.print("Person3 enter your weight in pounds: " );
weight3 = input.nextLine();
System.out.print("Enter your name: " );
person4 = input.nextLine();
System.out.print("Person4 enter your weight in pounds: " );
weight4 = input.nextLine();
System.out.print("Enter your name: " );
person5 = input.nextLine();
System.out.print("Person5 enter your weight in pounds: " );
weight5 = input.nextLine();
//convert average weights to kilograms (nearest tenth)
allPersons = person1, person2, person3, person4, person5; allWeight = weight1, weight2, weight3, weight4, weight5;
totalWeight = weight1 + weight2 + weight3 + weight4 + weight5;
avergeWeight = totalWeight/5;
addHalf = 0.05;
multiply = (* 10);
divide = (/ 10);
kiloWeight = averageWeight * KILO_CONVERSION + ADD_HALF + multiply + convert to integer + divide;
//output all original and new info
outputMessage = "Original names and weights in pounds are" allPersons + allWeights "there average weight in pounds equals" averageWeight "." /n;
"The total average weight" averageWeight "in kilograms equals" kiloWeight ".";
//print output to screen
System.out.println( outputMessage );
} // end main
} // end class
The errors that I am getting:
C:\JavaChp01\AverageWeight.java:18: not a statement
String person1; person2; person3; person4; person5; allPersons, outputMessage;
^
C:\JavaChp01\AverageWeight.java:18: not a statement
String person1; person2; person3; person4; person5; allPersons, outputMessage;
^
C:\JavaChp01\AverageWeight.java:18: not a statement
String person1; person2; person3; person4; person5; allPersons, outputMessage;
^
C:\JavaChp01\AverageWeight.java:18: not a statement
String person1; person2; person3; person4; person5; allPersons, outputMessage;
^
C:\JavaChp01\AverageWeight.java:18: not a statement
String person1; person2; person3; person4; person5; allPersons, outputMessage;
^
C:\JavaChp01\AverageWeight.java:18: ';' expected
String person1; person2; person3; person4; person5; allPersons, outputMessage;
^
C:\JavaChp01\AverageWeight.java:64: ';' expected
allPersons = person1, person2, person3, person4, person5;
^
C:\JavaChp01\AverageWeight.java:65: ';' expected
allWeight = weight1, weight2, weight3, weight4, weight5;
^
C:\JavaChp01\AverageWeight.java:69: illegal start of expression
multiply = (* 10);
^
C:\JavaChp01\AverageWeight.java:69: ')' expected
multiply = (* 10);
^
C:\JavaChp01\AverageWeight.java:70: illegal start of expression
divide = (/ 10);
^
C:\JavaChp01\AverageWeight.java:70: ')' expected
divide = (/ 10);
^
C:\JavaChp01\AverageWeight.java:71: ';' expected
kiloWeight = averageWeight * KILO_CONVERSION + ADD_HALF + multiply + convert to integer + divide;
^
C:\JavaChp01\AverageWeight.java:76: ';' expected
outputMessage = "Original names and weights in pounds are" allPersons + allWeights "there average weight in pounds equals" averageWeight "." /n;
^
C:\JavaChp01\AverageWeight.java:77: not a statement
"The total average weight" averageWeight "in kilograms equals" kiloWeight ".";
^
C:\JavaChp01\AverageWeight.java:77: ';' expected
"The total average weight" averageWeight "in kilograms equals" kiloWeight ".";
^
16 errors
Tool completed with exit code 1
I am stuck! Any advise to help me learn what I am doing wrong would be greatly appreciated. I
Thanks!