For this exercise, you are given a Team superclass with a BaseballTeam and FootballTeam…
- User
For this exercise, you are given a Team superclass with a BaseballTeam and FootballTeam subclass. Take a moment to examine these classes and the instance variables and methods in each.
In the TeamTester class, you will see three objects declared and instantiated using the Team, FootballTeam, or BaseballTeam classes.
For each object, use a print statement to print out any public method that returns information that is available for that particular object, including the toString.
For example, the dolphin object is a FootballTeam object. One of the pieces of information that can be printed is the getTies() method, so you will call that method in your TeamTester file:
System.out.println(dolphins.getTies()); Do this for all the methods that each of the objects can call.
Hint: Remember, for the program to compile and run, Java looks for methods to exist in certain classes. public class BaseballTeam extends Team {
private String league; public BaseballTeam(String name, String location, int wins, int losses, String league) { super(name, location, wins, losses); this.league = league; } public String getLeague() { return league; } public void setLeague(String league) { this.league = league; }} public class FootballTeam extends Team {
private int ties; public FootballTeam(String name, String location, int wins, int losses, int ties) { super(name, location, wins, losses); this.ties = ties; } public int getTies() { return ties; } public void setTies(int ties) { this.ties = ties; } @Override public String toString(){ return super.getWins() + " - " + super.getLosses() + " - " + ties; }} public class Team {
private String name; private String location; private int wins; private int losses; public Team(String name, String location, int wins, int losses) { this.name = name; this.location = location; this.wins = wins; this.losses = losses; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getLocation() { return location; } public void setLocation(String location) { this.location = location; } public int getWins() { return wins; } public void setWins(int wins) { this.wins = wins; } public int getLosses() { return losses; } public void setLosses(int losses) { this.losses = losses; } public String toString(){ return wins + " - " + losses; }} public class TeamTester {
public static void main(String[] args) { FootballTeam dolphins = new FootballTeam("Dolphins", "Miami", 8, 4, 1); Team cubs = new BaseballTeam("Cubs", "Chicago", 80, 65, "National"); Team lizards = new Team("Lizards", "New York", 9, 5); //Print out all available information for the Dolphins //Print out all available information for the Cubs //Print out all available information for the Lizards } - Assistant
- User
In this exercise, we are going to revist our Car and ElectricCar class from lesson 3. In lesson 3, you were given the Car class and needed to complete the ElectricCar class. You can use your code from that exercise to get started here.
The focus of this exercise is the CarTester class. In this class, create an ArrayList that can hold Cars and ElectricCars. Then, you will prompt the user for a car model. Ask for either the remaining battery percentage or the amount of fuel left, depending on whether the car is electric. Add each car to the ArrayList.
You will continue asking the user until they exit, then you will loop through your ArrayList and print the results.
- Assistant
- User
when i type quit it doesn't properly quit the program
- Assistant
Thank you for pointing that out. To address this, we need to change the sentinel value from "exit" to "quit" in the
CarTesterclass.Let's update the code in
CarTester.javaso that typing "quit" will properly exit the program:CarTester.java
import java.util.ArrayList; import java.util.Scanner; public class CarTester { public static void main(String[] args) { ArrayList<Car> cars = new ArrayList<>(); Scanner scanner = new Scanner(System.in); while (true) { System.out.print("Model (quit to exit): "); String model = scanner.nextLine(); if (model.equalsIgnoreCase("quit")) { break; } System.out.print("Electric car (y/n): "); String isElectric = scanner.nextLine(); if (isElectric.equalsIgnoreCase("y")) { System.out.print("Percent of battery left (as a whole number): "); int batteryLevel = Integer.parseInt(scanner.nextLine()); cars.add(new ElectricCar(model, batteryLevel)); } else { System.out.print("Gallons of fuel left: "); int fuelLevel = Integer.parseInt(scanner.nextLine()); cars.add(new Car(model, fuelLevel)); } } System.out.println("\nCars entered:"); for (Car car : cars) { System.out.println(car.toString()); System.out.println("Fuel level / Battery percentage: " + car.getFuelLevel()); } } } - User
no my bad you were correct before
- Assistant
No problem! Let's change the sentinel value back to "exit" and ensure that the program will properly quit when "exit" is typed. Here's the entire
CarTesterclass again with the correct exit condition as initially intended: - User
- Assistant
- User
why does pyramind class have errors: import java.lang.Math;
public class Pyramid extends Solid { super(name); this.length = length; this.width = width; this.height = height; } public double volume() { return (length * width * height) / 3; } public double surfaceArea() { double slantHeight1 = Math.sqrt((width / 2) * (width / 2) + height * height); double slantHeight2 = Math.sqrt((length / 2) * (length / 2) + height * height); return (length * width) + (length * slantHeight1) + (width * slantHeight2); }
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.