|
|
|
|
New Member
|
|
Jan 24, 2015, 12:26 PM
|
|
Java Program
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?
|
|
Check out some similar questions!
Java program help!!
[ 1 Answers ]
Dr.Java method and overloaded methods help in code?
Write a program that uses two overloaded methods to calculate the monthly payment for a loan. The formula for calculating a payment is shown further down.
Given that the user enters:
- an annual interest rate such as 3.600%, which is the...
Java netbeans program- Connection between java and mysql,How to conn
[ 0 Answers ]
which I wrote from a book but when I run it, it shows exception
please give me the code to connect jlistbox to database when I click on btn1.
btn coding should search "name" in database and shows all the names in listbox
my coding:
here-- r1,r2,r3 are radio btns and l1,l2,l3 are labels,...
Java Program
[ 2 Answers ]
1) Code a java class for an Object SONG, that has as a minimum the following attributes:- SONG Title (a String) , SONG Group/artist (a String ), SONG genre (a String – “Pop”, “Jazz”, “Hard Rock” etc. SONG price (double), SONG duration (int number of seconds). Code all accessor and mutator methods...
Java program?
[ 2 Answers ]
we have an assignment!
but hold up!
I would only ask what is the name of this code if I have fill up the blanks!
code:
puclic class problim{
public static void main(Stringarg){
int x=___;
int y=___;
while;(__<s){
x=____;
Java program error
[ 1 Answers ]
Sir, program is for managing account details using jdbc but ,there error like some variable not accessible in specefic block . Please solve this problem. If you can..
//mypanel1-LogIn
class mypanel1 extends JPanel
{
JTextField t1,t2;
JButton b1,b2;
mypanel1()
{
JLabel l1,l2; ...
View more questions
Search
Question Tools |
Search this Question |
|
|
Add your answer here.
|