PDA

View Full Version : Java Program


Zahars
Jan 24, 2015, 12:26 PM
I Cannot calculate the average per day of person

I want my code like this:

Student Daily spending
Name Day 1 Day 2 Day 3 Day 4 Day 5 Day 6 Day 7 Average per person
Joe Smith 42 53 20 0 0 43 92 40.00
Tommy Jones 20 30 0 10 0 98 9 23.86
Sara Lee 120 20 10 50 70 30 2 43.14
Average per day 60.66 34.33 10.00 20.00 23.33 67.00 34.33 35.67


here is my code:

import java.text.DecimalFormat;
import java.util.Scanner;
public class Marks {


private String [] name;
private int[][]scores;
private double[]studentAvg;
private double[]assignAvg;
//-----------------------------------------------------------------
// Constructor and gives a instantiated objects a value.
//-----------------------------------------------------------------
public Marks()
{
name=new String[3];//initialized the name of the string
scores = new int[3][7];//initialized the scores
studentAvg = new double[3];//initialized the student average
assignAvg = new double[7];//initialized the assigntment average


}
public void getInput()
{


Scanner scan = new Scanner(System.in);
// use for loop to print out the student name.
for (int I =0;i < name.length;i++)
{
System.out.println("Person Name: ");
name[i]=scan.nextLine();
}
// use the instance loop to caculate the average for the student and assignment
// to get the number from the user for the assignment.
for(int row = 0; row < scores.length; row++)
{
System.out.println("Enter the 7 number money spending each day for: " + name[row]);
for(int col = 0; col < scores[row].length; col++)
{
scores[row][col] = scan.nextInt();
studentAvg[row] += scores[row][col]/7.0;
assignAvg[col] += scores[row][col]/3.0;




}
}




}
//-----------------------------------------------------------------
// Returns a string representation of array data.
//-----------------------------------------------------------------
public String toString()// toString to display the output for the student assignment mark and average.
{
DecimalFormat fmt = new DecimalFormat ("0.##");//using decimalformat to 2 decimal.
String sta="";
String str=("Person \t\t\t Spending \t\t\t\t\t\t\t\t\t\t\t\t\t Average \n" +
"Name \n" +
"\t\t\t\t Day1 \t Day2 \t Day3 \t Day4 \t Day5 \t Day6 \t Day7");


for(int i = 0; i < scores.length; i++)
{


str+="\n" + name[i]+" \t";
for(int col = 0; col < scores[0].length; col++)
{


str +=" \t " + scores[i][col] + "\t ";


}
double avgS=studentAvg[i];
double avgA=assignAvg[i];
str += "\t\t\t" + fmt.format(avgS);
sta += "\t\t\t"+" " + fmt.format(avgA);




}


return str+"\nAverag" + sta;// return the String.
}


}
public class MarksDriver {


public static void main(String[] args) {
Marks m=new Marks();// declearing an instance of class Marks
m.getInput();
System.out.println(m);


}
}

where is my problem?