Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I keep getting this error while trying to run my java code. Please help me fix: import java.util.ArrayList; import java.util.Scanner; public class CMSC 2 1

I keep getting this error while trying to run my java code. Please help me fix: import java.util.ArrayList;
import java.util.Scanner;
public class CMSC215PROJ1NW {
class Height {
private final int feet;
private final int inches;
public Height(int feet, int inches){
this.feet = feet;
if (inches >=12){
this.inches = inches %12;
this.feet += inches /12;
} else {
this.inches = inches;
}
}
public int toInches(){
return feet *12+ inches;
}
@Override
public String toString(){
return feet +"'"+ inches +"\"";
}
}
class Player {
private final String name;
private final Height height;
private final int age;
public Player(String name, Height height, int age){
this.name = name;
this.height = height;
this.age = age;
}
public String getName(){
return name;
}
public Height getHeight(){
return height;
}
public int getAge(){
return age;
}
@Override
public String toString(){
return "Name: "+ name +", Height: "+ height.toString()+", Age: "+ age;
}
}
class InputData {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
ArrayList players = new ArrayList>();
while (true){
System.out.print("Enter player name (or done to finish): ");
String name = scanner.nextLine();
if (name.equalsIgnoreCase("done")){
break;
}
System.out.print("Enter height (feet): ");
int feet = scanner.nextInt();
System.out.print("Enter height (inches): ");
int inches = scanner.nextInt();
System.out.print("Enter age: ");
int age = scanner.nextInt();
scanner.nextLine(); // Consume newline
players.add(new Player(name, new Height(feet, inches), age));
}
int totalAge =0;
for (Player player : players){
totalAge += player.getAge();
}
double averageAge = totalAge /(double) players.size();
Player tallestPlayer = null;
for (Player player : players){
if (player.getAge()= averageAge){
if (tallestPlayer == null || player.getHeight().toInches()> tallestPlayer.getHeight().toInches()){
tallestPlayer = player;
}
}
}
System.out.println("Average age of all players: "+ averageAge);
if (tallestPlayer != null){
System.out.println("Tallest player whose age is less than or equal to the average age: "+ tallestPlayer);
} else {
System.out.println("No player found whose age is less than or equal to the average age.");
}
}
}
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Microsoft Visual Basic 2008 Comprehensive Concepts And Techniques

Authors: Gary B. Shelly, Corinne Hoisington

1st Edition

1423927168, 978-1423927167

More Books

Students also viewed these Databases questions

Question

c. What were you expected to do when you grew up?

Answered: 1 week ago

Question

d. How were you expected to contribute to family life?

Answered: 1 week ago

Question

e. What do you know about your ethnic background?

Answered: 1 week ago