# Another Calculation Average Grade program

Page 1 of 1

## 14 Replies - 8820 Views - Last Post: 09 September 2009 - 10:46 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=124144&amp;s=705ea3c8f52185456ffb7c6be58d1b99&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 TheNobleSword

Reputation: 2
• Posts: 57
• Joined: 05-September 09

# Another Calculation Average Grade program

Posted 05 September 2009 - 02:41 PM

Hello guys,

I started a Intro to java class about a week ago. I have not experience programming, and the text book is not much help.

This is what the project calls for: Input data for three students, each containing a first and last name and two exam scores. For each student, concatenate the first and last names separated by a space and calculate their average exam grade. Output the concatenated name followed by five spaces, their first exam score, 5 spaces, their second exam score, five spaces, and the individual's average exam score.After the data for each individual student has been output, output the overall average score for the group of students. If the overall average score is greater than 80, output the message "What an awesome class!". If the average is 80 or less but greater than 70, output the message "Not bad for the first exam." If the average is equal to or less than 70, output the message, "Looks like we need a new teacher!"

This is what I have so far. And most of it I have put together looking around this forum. I really appretiate your time and help. Remember I am about 20% done with this.

```package grades;

import java.util.Scanner;

public static void main(String[] args)
{
//create the variables
int testScore1;
int testScore2;
float averageScore;
float overallAverage;
String firstName;
String lastName;

//create keyboard for input
Scanner Keyboard = new Scanner(System.in);

System.out.println("Please enter students first name: ");

System.out.println("Please enter students last name: ");

//Ask the user for the 5 test scores
testScore1 = Keyboard.nextInt();

testScore2 = Keyboard.nextInt();

//Calculate the average score for the tests
averageScore = ((testScore1 + testScore2/2));

//Calcualte the over all average

overallAverage = (())

//Display students fist and last name concatenated

//Display Average test Score
System.out.println("The Students test scores were"  +testScore1+ testScore2);
System.out.println("The Students test scores were"  +testScore1+ testScore2);
System.out.println("The average score of the 2 tests is " + averageScore);

//Dispaly overall average

}

}

```

I am sure that is going to make most you gentlemen laugh!

thanks!

Is This A Good Question/Topic? 0

## Replies To: Another Calculation Average Grade program

### #2 mostyfriedman

• The Algorithmi

Reputation: 729
• Posts: 4,473
• Joined: 24-October 08

## Re: Another Calculation Average Grade program

Posted 05 September 2009 - 02:51 PM

just a correction
```averageScore = ((testScore1 + testScore2/2));

```

here you are adding testScore1 and half of testScore2 because division has a higher precedence than addition so the division of testScore2 by 2 will be done first then the addition to testScore1 will be applied..what you wanna do is add them both and then divide by 2..gotta watch for those parenthesis

```averageScore = ((testScore1 + testScore2)/2);

```

this should be correct now

### #3 mostyfriedman

• The Algorithmi

Reputation: 729
• Posts: 4,473
• Joined: 24-October 08

## Re: Another Calculation Average Grade program

Posted 05 September 2009 - 02:59 PM

now for the rest of the assignment, what i would do is write a Student class with the appropriate instance variables and instance method(this should be easy). and then maybe have another class called Classroom with an array of Student objects as an instance variable and in the constructor i would instantiate the Student objects with the grades and the whole thing. Then maybe you can add methods that would calculate the average grades for the students and what not..that was a pretty rough sketch of what a design may look like, so try it out and good luck

### #4 TheNobleSword

Reputation: 2
• Posts: 57
• Joined: 05-September 09

## Re: Another Calculation Average Grade program

Posted 05 September 2009 - 05:09 PM

mostyfriedman, on 5 Sep, 2009 - 01:59 PM, said:

now for the rest of the assignment, what i would do is write a Student class with the appropriate instance variables and instance method(this should be easy). and then maybe have another class called Classroom with an array of Student objects as an instance variable and in the constructor i would instantiate the Student objects with the grades and the whole thing. Then maybe you can add methods that would calculate the average grades for the students and what not..that was a pretty rough sketch of what a design may look like, so try it out and good luck

Thinks for the help! However, I forgot to mention that the instructor asked us not to use an array or loop. I can kind of wrap my head around writing the program for one student, but I am having trouble doing it for 3 students without using an array.

### #5 mostyfriedman

• The Algorithmi

Reputation: 729
• Posts: 4,473
• Joined: 24-October 08

## Re: Another Calculation Average Grade program

Posted 05 September 2009 - 05:23 PM

then you will just add up the scores and divide by 3

### #6 syfran

• D.I.C Lover

Reputation: 83
• Posts: 1,103
• Joined: 12-July 09

## Re: Another Calculation Average Grade program

Posted 05 September 2009 - 05:29 PM

Quote

I forgot to mention that the instructor asked us not to use an array or loop.

What did you do to piss him off?

### #7 TheNobleSword

Reputation: 2
• Posts: 57
• Joined: 05-September 09

## Re: Another Calculation Average Grade program

Posted 05 September 2009 - 05:31 PM

mostyfriedman, on 5 Sep, 2009 - 04:23 PM, said:

then you will just add up the scores and divide by 3

Yes, that would give me the average. I am not having trouble with the logic. I just don't know how to gather the user defined info and print it out the way I want.

firstname+lastname score#1 score#2 average
firstname+lastname score#1 score#2 average
firstname+lastname score#1 score#2 average

overall class average

if overall class average >70: statement
else: statement

I might not be asking the right questions. I am just not to sure.

### #8 TheNobleSword

Reputation: 2
• Posts: 57
• Joined: 05-September 09

## Re: Another Calculation Average Grade program

Posted 07 September 2009 - 10:17 AM

anyone, please. I really need help!

### #9 TheNobleSword

Reputation: 2
• Posts: 57
• Joined: 05-September 09

## Re: Another Calculation Average Grade program

Posted 08 September 2009 - 11:22 AM

Okay I have had some time to work on my project this weekend and this is what I have got so far.

My Code:

```package averagegrade;

import java.util.Scanner;

public static void main(String[] args)
{

Scanner keyboard=new Scanner(System.in);

System.out.println("Enter first students firstname:  ");
String firstname1;
firstname1=keyboard.next();

System.out.println("Enter first students lastname:  ");
String lastname1;
lastname1=keyboard.next();

System.out.println("Enter score #1:");
int score1;
score1=keyboard.nextInt();

System.out.println("Enter score #2:");
int score2;
score2=keyboard.nextInt();

System.out.println("Enter next students firstname:  ");
String firstname2;
firstname2=keyboard.next();

System.out.println("Enter next students lastname:  ");
String lastname2;
lastname2=keyboard.next();

System.out.println("Enter score #1:");
int score3;
score3=keyboard.nextInt();

System.out.println("Enter score #2:");
int score4;
score4=keyboard.nextInt();

System.out.println("Enter next students firstname:  ");
String firstname3;
firstname3=keyboard.next();

System.out.println("Enter next students lastname:  ");
String lastname3;
lastname3=keyboard.next();

System.out.println("Enter score #1:");
int score5;
score5=keyboard.nextInt();

System.out.println("Enter score #2:");
int score6;
score6=keyboard.nextInt();

System.out.println("Student 1:" +firstname1+lastname1+score1+score2);
System.out.println("Student 2:" +firstname2+lastname2+score3+score4);
System.out.println("Student 3:" +firstname3+lastname3+score5+score6);

System.out.println("the class average is:" );

}

}

```

This is my Current output:

```Enter first students firstname:
John
Enter first students lastname:
Jacob
Enter score #1:
65
Enter score #2:
89
Enter next students firstname:
Albert
Enter next students lastname:
Einstien
Enter score #1:
95
Enter score #2:
97
Enter next students firstname:
Thomas
Enter next students lastname:
Edison
Enter score #1:
84
Enter score #2:
68
Student 1:JohnJacob6589
Student 2:AlbertEinstien9597
Student 3:ThomasEdison8468
the class average is:

```

I do not know how to add spaces (I need 5) in between the data and I am not sure how to implement the average calculation.

I tried using:

```float averageScore=((score1+score2+score3+score4+score5+score6)/3);

System.out.println("The class average is :" + averageScore);

```

But that did not work for me.

Any ideas guys, please, I am trying!

### #10 mostyfriedman

• The Algorithmi

Reputation: 729
• Posts: 4,473
• Joined: 24-October 08

## Re: Another Calculation Average Grade program

Posted 08 September 2009 - 11:31 AM

you just have a lil part left, you could do something like this

```float sum1 = score1+score2;
float sum2 = score3+score4;
float sum3 = score5+score6;

System.out.println("Student 1: " +firstname1+ " "+ lastname1+ " " + sum1);
System.out.println("Student 2: " +firstname2+ " "+lastname2+" "+ sum2);
System.out.println("Student 3: " +firstname3+" " + lastname3+" "+ sum3);
System.out.println("the class average is: " + ((sum1+sum2+sum3)/6));

```

there you go

edited for typos

This post has been edited by mostyfriedman: 08 September 2009 - 11:38 AM

### #11 TheNobleSword

Reputation: 2
• Posts: 57
• Joined: 05-September 09

## Re: Another Calculation Average Grade program

Posted 08 September 2009 - 12:42 PM

Thanks so much! That seems to work.

```System.out.println("Student 1: " +firstname1+ " "+ lastname1+ " " + sum1);
```

That gave me the sum of score1 and score2, I needed them as two separate outputs. I just separated them.

Then I needed the students personal average at the end, so I changed "println" to "print" and just did that computation on the next line.

Then I added an if-else statement.

and I have one more question.

For the personal average I just wanted the "average" to be output, So I did this

```System.out.println(" "+average3);
```

is that correct, with the empty ""?

Thanks again for so much help! I am starting to get it!

Code

```package averagegrade;

import java.util.Scanner;

public static void main(String[] args)
{

Scanner keyboard=new Scanner(System.in);

System.out.println("Enter first students firstname:  ");
String firstname1;
firstname1=keyboard.next();

System.out.println("Enter first students lastname:  ");
String lastname1;
lastname1=keyboard.next();

System.out.println("Enter score #1:");
int score1;
score1=keyboard.nextInt();

System.out.println("Enter score #2:");
int score2;
score2=keyboard.nextInt();

System.out.println("Enter next students firstname:  ");
String firstname2;
firstname2=keyboard.next();

System.out.println("Enter next students lastname:  ");
String lastname2;
lastname2=keyboard.next();

System.out.println("Enter score #1:");
int score3;
score3=keyboard.nextInt();

System.out.println("Enter score #2:");
int score4;
score4=keyboard.nextInt();

System.out.println("Enter next students firstname:  ");
String firstname3;
firstname3=keyboard.next();

System.out.println("Enter next students lastname:  ");
String lastname3;
lastname3=keyboard.next();

System.out.println("Enter score #1:");
int score5;
score5=keyboard.nextInt();

System.out.println("Enter score #2:");
int score6;
score6=keyboard.nextInt();

float sum1 = score1+score2;
float sum2 = score3+score4;
float sum3 = score5+score6;
float average1 = (sum1)/2;
float average2 = (sum2)/2;
float average3= (sum3)/2;
float average = (sum1+sum2+sum3)/6;

System.out.print("Student 1: " +firstname1+ " "+ lastname1+ " " + score1+ " "+ score2);
System.out.println(" "+average1);
System.out.print("Student 2: " +firstname2+ " "+lastname2+" " + score3+ " "+ score4);
System.out.println(" "+average2);
System.out.print("Student 3: " +firstname3+" " + lastname3+" " + score5+ " "+ score6);
System.out.println(" "+average3);
System.out.println("The class average is: " + average);

if (average > 80)
System.out.println("What an awesome class!");
else if (average > 70)
System.out.println("Not bad for the first exam.");
else
System.out.println("Looks like we need a new teacher!");

}

}

```

This post has been edited by TheNobleSword: 08 September 2009 - 12:43 PM

### #12 mostyfriedman

• The Algorithmi

Reputation: 729
• Posts: 4,473
• Joined: 24-October 08

## Re: Another Calculation Average Grade program

Posted 08 September 2009 - 12:54 PM

do you want to print the average value or the string average??.if you just want to print the average value then you dont need to concatenate it to a string
```System.out.println(average);

```

### #13 TheNobleSword

Reputation: 2
• Posts: 57
• Joined: 05-September 09

## Re: Another Calculation Average Grade program

Posted 08 September 2009 - 01:07 PM

that makes more sense, I knew something was wrong.

I realised I could just add it to the end of the last line like this

``` System.out.println(firstname1+ " " + lastname1+ " " + score1+ " " + score2+ " " +average1);
System.out.println(firstname2+ " " +lastname2+ " " + score3+ " " + score4+ " " +average2);
System.out.println(firstname3+ " " + lastname3+ " " + score5+ " " + score6+ " " +average3);
System.out.println("The class average is:" +average);

```

thanks again. Hopefully someday I can return the favor.

### #14 mostyfriedman

• The Algorithmi

Reputation: 729
• Posts: 4,473
• Joined: 24-October 08

## Re: Another Calculation Average Grade program

Posted 08 September 2009 - 01:29 PM

Quote

thanks again. Hopefully someday I can return the favor.

no need to do that, that's what we do here at DIC..besides what's the use if you have some little knowledge and you don't pass it on to other people

### #15 TheNobleSword

Reputation: 2
• Posts: 57
• Joined: 05-September 09

## Re: Another Calculation Average Grade program

Posted 09 September 2009 - 10:46 AM

Thanks mosty!

This post has been edited by TheNobleSword: 09 September 2009 - 10:47 AM