Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Could somebody help me with this program? The code does work but I am wondering

ID: 3796053 • Letter: C

Question

Could somebody help me with this program?

The code does work but I am wondering how I would be able to change the program so it doesn't use a Hashmap because it CAN'T use it 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
{
String bowlerNumber;
int score1,score2,score3;

//gets the bowler number
public String getBowlerNumber()
{
return bowlerNumber;
}

//sets the bowler number
public void setBowlerNumber(String 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.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;
import java.util.Scanner;
public class BowlerReports {
  
//creates an array list called Bowler
static HashMap<Character, ArrayList<Bowler>> map;

public static void main(String args[])
{
ArrayList<Bowler> scores;


map = new HashMap<Character, ArrayList<Bowler>>();
try{
  
//creates a scanner
Scanner s = new Scanner(System.in);

//opens the file Scores.txt
FileReader fr = new FileReader("Scores.txt");

//Reads the file
BufferedReader br= new BufferedReader(fr);
String line;

//creates a string buffer
StringBuffer sb = new StringBuffer();

//stores all of the info from the file into line
while((line=br.readLine())!=null)
{
sb.append(line);
}

//Converting into string and splitting using comma
String data[] = sb.toString().split("," + " ");
int i = 0;
Bowler b;
  
while(data.length > i)
{
//stores the Bowler array list in scores
scores = new ArrayList<Bowler>();
b = new Bowler();
b.setBowlerNumber(data[i]);

//takes all of the bowlers that have a number in the 100s
b.setScore1(Integer.parseInt(data[i+1]));

//takes all of the bowlers that have a number in the 200s
b.setScore2(Integer.parseInt(data[i+2]));

//takes all of the bowlers that have a number in the 300s
b.setScore3(Integer.parseInt(data[i+3]));

//takes all of the bowler that have a number in the 400s
i=i+4;

scores.add(b);

//adds the number to the list according to the number it starts with
if(map.containsKey(b.getBowlerNumber().charAt(0))){
ArrayList<Bowler> sl = map.get(b.getBowlerNumber().charAt(0));
sl.add(b);
map.put(b.getBowlerNumber().charAt(0), sl);
}

else
{
//adds the number normally
map.put(b.getBowlerNumber().charAt(0),scores);
}
}

BowlerReports bReport = new BowlerReports();

int j = 0;
while(j == 0)
{
//asks the user to select what report they would like
System.out.println("Please select what report you would like");

System.out.println("1. A report with all of the bowlers and their scores");

System.out.println("2. A report with all of the bowlers scores and their averages");

System.out.println("3. A repot with all of the bowlers and their team totals");

System.out.println("4. To exit the program");

//creates a scanner to record the users input
Scanner sc = new Scanner(System.in);

//stores the users input
int choice = sc.nextInt();

//chooses what to do with the users input
switch(choice)
{
//shows the Bowlers numbers and scores
case 1: bReport.generateReport(map);
break;
  
//shows the Bowlers scores and average scores
case 2: bReport.generateReportAvg(map);
break;
  
//shows the Bowlers scores and their team scores
case 3: bReport.generateReportGT(map);
break;

// exits the program
case 4: j = 1;
System.out.println("See ya later!");
break;
  
//if the user enters an invalid entry then it propmts them to enter in the correct entry
default: System.out.println("A number between 1 and 4");
}   
}
}
catch(Exception e)
{
e.printStackTrace();
}
  
}

private void generateReport(Character teamName)
{
ArrayList<Bowler> als = map.get(teamName);
Iterator<Bowler> bow = als.iterator();

while(bow.hasNext())
{
Bowler bb = bow.next();
  
//shows the bowler number and their three scores
System.out.println("Bowler " + bb.getBowlerNumber() + " scores are: " +" "+bb.getScore1()+", "+bb.getScore2()+", "+bb.getScore3());

System.out.println();
}
}

private void generateReportAvg(Character average)
{
ArrayList<Bowler> als = map.get(average);
Iterator<Bowler> bow = als.iterator();
int sum1,sum2,sum3;
sum1=sum2=sum3=0;

while(bow.hasNext())
{
Bowler bb = bow.next();

//caculates the average score for the bowlers
float avg = (bb.getScore1()+bb.getScore2()+bb.getScore3())/3;

//displays the bowlers number and their three scores
System.out.println("Bowler " + bb.getBowlerNumber() +" game scores are: "+bb.getScore1()+", "+bb.getScore2()+", "+bb.getScore3());

//displays the bowlers number and thier average
System.out.println("Average Score of bowler "+bb.getBowlerNumber()+" is: "+avg);

System.out.println();

sum1 = sum1 + bb.getScore1();
sum2 = sum2 + bb.getScore2();
sum3 = sum3 + bb.getScore3();
}
}

private void generateReportGT(Character GrandTotal) {
ArrayList<Bowler> als = map.get(GrandTotal);
Iterator<Bowler> bow = als.iterator();
int t = 0;
int gt = 0;
while(bow.hasNext())
{
Bowler bb = bow.next();

//adds up the bowlers scores and stores them in t
t = t + bb.getScore1()+bb.getScore2()+bb.getScore3();

//displays the bowlers number and thier three scores
System.out.println("Bowler " + bb.getBowlerNumber() +" games score are: "+bb.getScore1()+", "+bb.getScore2()+", "+bb.getScore3());

//takes the bowlers total scores and adds them all together
gt = gt + t;

//displays the total score for each bowler
System.out.println("Bowler's total score is: "+t);

System.out.println();
}

//displays the Grand total for the teams scores
System.out.println("Grand total for the team is: "+ gt);

System.out.println();
}

public void generateReport(HashMap<Character, ArrayList<Bowler>> map) {
// Getting Key and Value from HashMap using entrySet
//Iterating each Bowler using Entry in Map
Set<Entry<Character, ArrayList<Bowler>>> set = map.entrySet();
Iterator<Entry<Character, ArrayList<Bowler>>> itr = set.iterator();
while(itr.hasNext()){
Entry<Character, ArrayList<Bowler>> entry = itr.next();
this.generateReport(entry.getKey());
}
}

public void generateReportAvg(HashMap<Character, ArrayList<Bowler>> map) {
// Getting Key and Value from HashMap using entrySet
//Iterating each Bowler using Entry in Map
Set<Entry<Character, ArrayList<Bowler>>> set2 = map.entrySet();
Iterator<Entry<Character, ArrayList<Bowler>>> itr2 = set2.iterator();
while(itr2.hasNext()){
Entry<Character, ArrayList<Bowler>> entry2 = itr2.next();
this.generateReportAvg(entry2.getKey());
}
}

public void generateReportGT(HashMap<Character, ArrayList<Bowler>> map) {
// Getting Key and Value from HashMap using entrySet
//Iterating each Bowler using Entry in Map
Set<Entry<Character, ArrayList<Bowler>>> set3 = map.entrySet();
Iterator<Entry<Character, ArrayList<Bowler>>> itr3 = set3.iterator();
while(itr3.hasNext()){
Entry<Character, ArrayList<Bowler>> entry3 = itr3.next();
this.generateReportGT(entry3.getKey());
}
}
}

Any help would be greatly appreciated.

Explanation / Answer

Bowler Modal class with few modifications as per the requirements.

Bowler.java


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;
}
  
}

Execution part:

Run.java

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("c://users/shivakumar/desktop/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 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");
  
   choice=obj.nextInt();
  
   // Switch to select the users selection
   switch(choice)
   {
  
   // printing file information
  
   case 1: System.out.println("File information");
   System.out.println("file name:"+file.getName());
   System.out.println("file abs path:"+file.getAbsolutePath());
   System.out.println("file total space:"+file.getTotalSpace());
   break;
   case 2:
       System.out.println("File information");
       System.out.println("file name:"+file.getName());
       System.out.println("file abs path:"+file.getAbsolutePath());
       System.out.println("file total space:"+file.getTotalSpace());
       System.out.println();
      
       // Loop to calculate avg and print for every bowler
       for(int i=0;i<bowlers.length;i++)
       {
           System.out.println("Avg score of bowler"+bowlers[i].getBowlerNumber()+" is:"+(bowlers[i].score1+bowlers[i].score2+bowlers[i].score3)/3);
          
       }
       break;
   case 3: int sum=0,tot=0;
       System.out.println("File information");
       System.out.println("file name:"+file.getName());
       System.out.println("file abs path:"+file.getAbsolutePath());
       System.out.println("file total space:"+file.getTotalSpace());
       System.out.println();
      
       // Calculating sum or toatl scores
       for(int i=0;i<bowlers.length;i++)
       {
           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();
       System.out.println("Sum of series all scores is:"+sum);
       break;
       default: System.out.println("Invalid input");
   }
      
  
       }catch (Exception e) {
           // TODO: handle exception
           e.printStackTrace();
          
       }
      
   }
      
}