Could somebody help me with this program? The code does work but I am wondering
ID: 3796401 • Letter: C
Question
Could somebody help me with this program?
The code does work but I am wondering how I would how I would be able to make it find the grand total for each team in example for team 101-105 it would add up all of the scores and get 2151 (provided I added that all up right) and same for all of the other teams.I am thinking that just putting an array in each of the if statments to store all of the totals for each player would work and then adding everything in the array up to get the teams grandtotal, but I am not sure how to do that. It CAN'T use a Hashmap becuase we have not covered that in our class yet. The most recent things we have covered are arrays, wrapper classes, and inheritance / Polymorphism so the program cant use more advanced code than that if that helps you understand what I would need at all. The scores.txt is: 101, 123, 133, 165, 102, 202, 175, 220, 103, 155, 170, 140, 104, 123, 111, 99, 105, 75, 127, 133, 201, 195, 202, 187, 202, 188, 167, 175, 203, 145, 155, 165, 204, 165, 180, 133, 205, 140, 130, 125, 301, 100, 175, 90, 302, 130, 77, 120, 303, 200, 230, 193, 304, 178, 188, 193, 305, 155, 156, 157, 401, 160, 140, 155, 402, 170, 190, 185, 403, 210, 202, 233, 404, 105, 121, 133, 405, 145, 165, 185 and the numbers aren't allowed to be changed in anyway once so ever either. here is what the program should be able to do:
Problem overview: You are to write programs to help manage and provide reports for a small bowling league. Input specifications: Input will consist of a text file that is comma delimited. Each record will consist of bowler number, bowler first score, bowler second score, bowler third score. You are not allowed to modify the input file in any way. Processing requirement: Each record’s information will be used to instantiate a “Bowler” type object. The Bowler object will have the following attributes: bowlerNumber, score 1, score 2 and score 3. All attributes are to be integers. You will need to provide the ‘set’ and ‘get’ methods for each attribute. You will also need to provide at least 1 constructor method for the Bowler class. Output specifications: Your program needs to allow the user to choose between multiple reports. - Display file information - Display file information plus the average score for each bowler - Display file information plus the team totals for each game and a grand total for the team’s series total.
The code I have is this:
public class Bowler
{
int bowlerNumber;
int score1,score2,score3;
// Constructor
public Bowler(int bowlerNumber,int score1,int score2,int score3)
{
this.bowlerNumber=bowlerNumber;
this.score1=score1;
this.score2=score2;
this.score3=score3;
}
//gets the bowler number
public int getBowlerNumber()
{
return bowlerNumber;
}
//sets the bowler number
public void setBowlerNumber(int bowlerNumber)
{
this.bowlerNumber = bowlerNumber;
}
//gets the first score
public int getScore1()
{
return score1;
}
//sets the first score
public void setScore1(int score1)
{
this.score1 = score1;
}
//gets the second score
public int getScore2()
{
return score2;
}
//sets the second score
public void setScore2(int score2)
{
this.score2 = score2;
}
//gets the third score
public int getScore3()
{
return score3;
}
//sets the third score
public void setScore3(int score3)
{
this.score3 = score3;
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Scanner;
public class Run {
// Main execution goes here
public static void main(String[] args)
{
// File for the data
File file=new File("Scores.txt");
// Scanner obj to read user data when selecting report type
Scanner obj=new Scanner(System.in);
try
{
// Buffered reader to read the data
BufferedReader br=new BufferedReader(new FileReader(file));
// splitting string
String[] arr=br.readLine().split(","+" ");
br.close();
// Since there are four numbers for an object calculating the numebr of objects first
// Arrays are fixed in size
int numOfObjects=arr.length/4;
// Here array of Bowler objects
Bowler[] bowlers=new Bowler[numOfObjects];
int index=0;
// Creating and loading objects to bowler array
for(int i=0;i<arr.length-3;i=i+4)
{
bowlers[index]=new Bowler(Integer.parseInt(arr[i]),Integer.parseInt(arr[i+1]),Integer.parseInt(arr[i+2]),Integer.parseInt(arr[i+3]));
index++;
}
int j = 0;
while(j == 0)
{
int choice;
// Menu for the user
System.out.println(" ----- Report Menu----- ");
System.out.println("Enter 1: for Display file information.");
System.out.println("Enter 2: for Display file information plus. avg score for each bowler.");
System.out.println("Enter 3: for Display file information plus. team total");
System.out.println("Enter 4: to exit the program");
choice=obj.nextInt();
// Switch to select the users selection
switch(choice)
{
// printing file information
case 1:
System.out.println();
for(int i=0;i<bowlers.length;i++)
{
System.out.println("Scores for " + bowlers[i].getBowlerNumber() + " are: " + bowlers[i].score1 + ", " + bowlers[i].score2 + ", " + bowlers[i].score3);
System.out.println();
}
break;
case 2:
System.out.println();
// Loop to calculate avg and print for every bowler
for(int i=0;i<bowlers.length;i++)
{
System.out.println("Scores for bowler " + bowlers[i].getBowlerNumber() + " are: " + bowlers[i].score1 + ", " + bowlers[i].score2 + ", " + bowlers[i].score3);
System.out.println("Avg score of bowler: " +bowlers[i].getBowlerNumber() + " is: "+(bowlers[i].score1+bowlers[i].score2+bowlers[i].score3)/3);
System.out.println();
}
break;
case 3: int sum=0,tot=0;
System.out.println();
// Calculating sum or toatl scores
for(int i=0;i<bowlers.length;i++)
{
if (bowlers[i].getBowlerNumber() > 100 && bowlers[i].getBowlerNumber() < 200)
{
System.out.println("Scores for " + bowlers[i].getBowlerNumber() + " are: " + bowlers[i].score1 + ", " + bowlers[i].score2 + ", " + bowlers[i].score3);
tot=bowlers[i].score1+bowlers[i].score2+bowlers[i].score3;
System.out.println("Sum score of bowler "+bowlers[i].getBowlerNumber()+" is: "+tot);
System.out.println();
System.out.println("this is 100");
System.out.println();
}
if (bowlers[i].getBowlerNumber() > 200 && bowlers[i].getBowlerNumber() < 300)
{
System.out.println("Scores for " + bowlers[i].getBowlerNumber() + " are: " + bowlers[i].score1 + ", " + bowlers[i].score2 + ", " + bowlers[i].score3);
tot=bowlers[i].score1+bowlers[i].score2+bowlers[i].score3;
System.out.println("Sum score of bowler "+bowlers[i].getBowlerNumber()+" is: "+tot);
sum=sum+tot;
System.out.println("this is 200");
System.out.println();
}
if (bowlers[i].getBowlerNumber() > 300 && bowlers[i].getBowlerNumber() < 400)
{
System.out.println("Scores for " + bowlers[i].getBowlerNumber() + " are: " + bowlers[i].score1 + ", " + bowlers[i].score2 + ", " + bowlers[i].score3);
tot=bowlers[i].score1+bowlers[i].score2+bowlers[i].score3;
System.out.println("Sum score of bowler "+bowlers[i].getBowlerNumber()+" is: "+tot);
sum=sum+tot;
System.out.println("this is 300");
System.out.println();
}
if (bowlers[i].getBowlerNumber() > 400)
{
System.out.println("Scores for " + bowlers[i].getBowlerNumber() + " are: " + bowlers[i].score1 + ", " + bowlers[i].score2 + ", " + bowlers[i].score3);
tot=bowlers[i].score1+bowlers[i].score2+bowlers[i].score3;
System.out.println("Sum score of bowler "+bowlers[i].getBowlerNumber()+" is: "+tot);
sum=sum+tot;
System.out.println("this is 400");
System.out.println();
}
}
System.out.println();
System.out.println();
break;
case 4: j = 1;
System.out.println("See ya later!");
break;
default: System.out.println("Please enter a number 1-4");
System.out.println();
}
}
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
Any help would be greatly appreciated.
Explanation / Answer
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Scanner;
public class Run {
// Main execution goes here
public static void main(String[] args) {
// File for the data
File file = new File("D:\Scores.txt");
// Scanner obj to read user data when selecting report type
Scanner obj = new Scanner(System.in);
try {
// Buffered reader to read the data
BufferedReader br = new BufferedReader(new FileReader(file));
// splitting string
String[] arr = br.readLine().split("," + " ");
br.close();
// Since there are four numbers for an object calculating the numebr
// of objects first
// Arrays are fixed in size
int numOfObjects = arr.length / 4;
// Here array of Bowler objects
Bowler[] bowlers = new Bowler[numOfObjects];
int index = 0;
// Creating and loading objects to bowler array
for (int i = 0; i < arr.length - 3; i = i + 4) {
bowlers[index] = new Bowler(Integer.parseInt(arr[i]),
Integer.parseInt(arr[i + 1]),
Integer.parseInt(arr[i + 2]),
Integer.parseInt(arr[i + 3]));
index++;
}
int j = 0;
while (j == 0) {
int choice;
// Menu for the user
System.out.println(" ----- Report Menu----- ");
System.out.println("Enter 1: for Display file information.");
System.out
.println("Enter 2: for Display file information plus. avg score for each bowler.");
System.out
.println("Enter 3: for Display file information plus. team total");
System.out.println("Enter 4: to exit the program");
choice = obj.nextInt();
// Switch to select the users selection
switch (choice) {
// printing file information
case 1:
System.out.println();
for (int i = 0; i < bowlers.length; i++) {
System.out.println("Scores for "
+ bowlers[i].getBowlerNumber() + " are: "
+ bowlers[i].score1 + ", " + bowlers[i].score2
+ ", " + bowlers[i].score3);
System.out.println();
}
break;
case 2:
System.out.println();
// Loop to calculate avg and print for every bowler
for (int i = 0; i < bowlers.length; i++) {
System.out.println("Scores for bowler "
+ bowlers[i].getBowlerNumber() + " are: "
+ bowlers[i].score1 + ", " + bowlers[i].score2
+ ", " + bowlers[i].score3);
System.out
.println("Avg score of bowler: "
+ bowlers[i].getBowlerNumber()
+ " is: "
+ (bowlers[i].score1
+ bowlers[i].score2 + bowlers[i].score3)
/ 3);
System.out.println();
}
break;
case 3:
int sum1 = 0,
tot1 = 0;
int sum2 = 0,
tot2 = 0;
int sum3 = 0,
tot3 = 0;
int sum4 = 0,
tot4 = 0;
int grandtotal = 0;
int[] teamTotal = new int[4];
System.out.println();
// Calculating sum or toatl scores
for (int i = 0; i < bowlers.length; i++) {
if (bowlers[i].getBowlerNumber() > 100
&& bowlers[i].getBowlerNumber() < 200) {
System.out.println("Scores for "
+ bowlers[i].getBowlerNumber() + " are: "
+ bowlers[i].score1 + ", "
+ bowlers[i].score2 + ", "
+ bowlers[i].score3);
tot1 = bowlers[i].score1 + bowlers[i].score2
+ bowlers[i].score3;
System.out.println("Sum score of bowler "
+ bowlers[i].getBowlerNumber() + " is: "
+ tot1);
sum1 = sum1 + tot1;
System.out.println();
System.out.println("this is 100");
System.out.println();
}
teamTotal[0] = sum1;
if (bowlers[i].getBowlerNumber() > 200
&& bowlers[i].getBowlerNumber() < 300) {
System.out.println("Scores for "
+ bowlers[i].getBowlerNumber() + " are: "
+ bowlers[i].score1 + ", "
+ bowlers[i].score2 + ", "
+ bowlers[i].score3);
tot2 = bowlers[i].score1 + bowlers[i].score2
+ bowlers[i].score3;
System.out.println("Sum score of bowler "
+ bowlers[i].getBowlerNumber() + " is: "
+ tot2);
sum2 = sum2 + tot2;
System.out.println("this is 200");
System.out.println();
}
teamTotal[1] = sum2;
if (bowlers[i].getBowlerNumber() > 300
&& bowlers[i].getBowlerNumber() < 400) {
System.out.println("Scores for "
+ bowlers[i].getBowlerNumber() + " are: "
+ bowlers[i].score1 + ", "
+ bowlers[i].score2 + ", "
+ bowlers[i].score3);
tot3 = bowlers[i].score1 + bowlers[i].score2
+ bowlers[i].score3;
System.out.println("Sum score of bowler "
+ bowlers[i].getBowlerNumber() + " is: "
+ tot3);
sum3 = sum3 + tot3;
System.out.println("this is 300");
System.out.println();
}
teamTotal[2] = sum3;
if (bowlers[i].getBowlerNumber() > 400) {
System.out.println("Scores for "
+ bowlers[i].getBowlerNumber() + " are: "
+ bowlers[i].score1 + ", "
+ bowlers[i].score2 + ", "
+ bowlers[i].score3);
tot4 = bowlers[i].score1 + bowlers[i].score2
+ bowlers[i].score3;
System.out.println("Sum score of bowler "
+ bowlers[i].getBowlerNumber() + " is: "
+ tot4);
sum4 = sum4 + tot4;
System.out.println("this is 400");
System.out.println();
}
}
teamTotal[3] = sum4;
int count = 0;
for (int team : teamTotal) {
count = count + 100;
System.out.println("the total of team " + count
+ " is " + team);
}
System.out.println();
System.out.println();
break;
case 4:
j = 1;
System.out.println("See ya later!");
break;
default:
System.out.println("Please enter a number 1-4");
System.out.println();
}
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
output
----- Report Menu-----
Enter 1: for Display file information.
Enter 2: for Display file information plus. avg score for each bowler.
Enter 3: for Display file information plus. team total
Enter 4: to exit the program
1
Scores for 101 are: 123, 133, 165
Scores for 102 are: 202, 175, 220
Scores for 103 are: 155, 170, 140
Scores for 104 are: 123, 111, 99
Scores for 105 are: 75, 127, 133
Scores for 201 are: 195, 202, 187
Scores for 202 are: 188, 167, 175
Scores for 203 are: 145, 155, 165
Scores for 204 are: 165, 180, 133
Scores for 205 are: 140, 130, 125
Scores for 301 are: 100, 175, 90
Scores for 302 are: 130, 77, 120
Scores for 303 are: 200, 230, 193
Scores for 304 are: 178, 188, 193
Scores for 305 are: 155, 156, 157
Scores for 401 are: 160, 140, 155
Scores for 402 are: 170, 190, 185
Scores for 403 are: 210, 202, 233
Scores for 404 are: 105, 121, 133
Scores for 405 are: 145, 165, 185
----- Report Menu-----
Enter 1: for Display file information.
Enter 2: for Display file information plus. avg score for each bowler.
Enter 3: for Display file information plus. team total
Enter 4: to exit the program
2
Scores for bowler 101 are: 123, 133, 165
Avg score of bowler: 101 is: 140
Scores for bowler 102 are: 202, 175, 220
Avg score of bowler: 102 is: 199
Scores for bowler 103 are: 155, 170, 140
Avg score of bowler: 103 is: 155
Scores for bowler 104 are: 123, 111, 99
Avg score of bowler: 104 is: 111
Scores for bowler 105 are: 75, 127, 133
Avg score of bowler: 105 is: 111
Scores for bowler 201 are: 195, 202, 187
Avg score of bowler: 201 is: 194
Scores for bowler 202 are: 188, 167, 175
Avg score of bowler: 202 is: 176
Scores for bowler 203 are: 145, 155, 165
Avg score of bowler: 203 is: 155
Scores for bowler 204 are: 165, 180, 133
Avg score of bowler: 204 is: 159
Scores for bowler 205 are: 140, 130, 125
Avg score of bowler: 205 is: 131
Scores for bowler 301 are: 100, 175, 90
Avg score of bowler: 301 is: 121
Scores for bowler 302 are: 130, 77, 120
Avg score of bowler: 302 is: 109
Scores for bowler 303 are: 200, 230, 193
Avg score of bowler: 303 is: 207
Scores for bowler 304 are: 178, 188, 193
Avg score of bowler: 304 is: 186
Scores for bowler 305 are: 155, 156, 157
Avg score of bowler: 305 is: 156
Scores for bowler 401 are: 160, 140, 155
Avg score of bowler: 401 is: 151
Scores for bowler 402 are: 170, 190, 185
Avg score of bowler: 402 is: 181
Scores for bowler 403 are: 210, 202, 233
Avg score of bowler: 403 is: 215
Scores for bowler 404 are: 105, 121, 133
Avg score of bowler: 404 is: 119
Scores for bowler 405 are: 145, 165, 185
Avg score of bowler: 405 is: 165
----- Report Menu-----
Enter 1: for Display file information.
Enter 2: for Display file information plus. avg score for each bowler.
Enter 3: for Display file information plus. team total
Enter 4: to exit the program
3
Scores for 101 are: 123, 133, 165
Sum score of bowler 101 is: 421
this is 100
Scores for 102 are: 202, 175, 220
Sum score of bowler 102 is: 597
this is 100
Scores for 103 are: 155, 170, 140
Sum score of bowler 103 is: 465
this is 100
Scores for 104 are: 123, 111, 99
Sum score of bowler 104 is: 333
this is 100
Scores for 105 are: 75, 127, 133
Sum score of bowler 105 is: 335
this is 100
Scores for 201 are: 195, 202, 187
Sum score of bowler 201 is: 584
this is 200
Scores for 202 are: 188, 167, 175
Sum score of bowler 202 is: 530
this is 200
Scores for 203 are: 145, 155, 165
Sum score of bowler 203 is: 465
this is 200
Scores for 204 are: 165, 180, 133
Sum score of bowler 204 is: 478
this is 200
Scores for 205 are: 140, 130, 125
Sum score of bowler 205 is: 395
this is 200
Scores for 301 are: 100, 175, 90
Sum score of bowler 301 is: 365
this is 300
Scores for 302 are: 130, 77, 120
Sum score of bowler 302 is: 327
this is 300
Scores for 303 are: 200, 230, 193
Sum score of bowler 303 is: 623
this is 300
Scores for 304 are: 178, 188, 193
Sum score of bowler 304 is: 559
this is 300
Scores for 305 are: 155, 156, 157
Sum score of bowler 305 is: 468
this is 300
Scores for 401 are: 160, 140, 155
Sum score of bowler 401 is: 455
this is 400
Scores for 402 are: 170, 190, 185
Sum score of bowler 402 is: 545
this is 400
Scores for 403 are: 210, 202, 233
Sum score of bowler 403 is: 645
this is 400
Scores for 404 are: 105, 121, 133
Sum score of bowler 404 is: 359
this is 400
Scores for 405 are: 145, 165, 185
Sum score of bowler 405 is: 495
this is 400
the total of team 100 is 2151
the total of team 200 is 2452
the total of team 300 is 2342
the total of team 400 is 2499
----- Report Menu-----
Enter 1: for Display file information.
Enter 2: for Display file information plus. avg score for each bowler.
Enter 3: for Display file information plus. team total
Enter 4: to exit the program
4
See ya later!