how to add input into inheritance - java

Sorry if this is a little confusing, First time using this site so I dont know if I entered my code in right or not. So my question is, when I run this program in class driver I get this:
name: No Name Yet
Manufacturer: no name yet
Engine Cyclinders: 0
Load: 0.0
Towing: 0.0
Which is the default, now I am trying to be able to put a input in the class driver that will change say the name to whatever I set it as from class Person for example. I have tried doing p.setName("Tim") but it will only show the name as tim if I call the writeouput method from class Person. If I call the writeoutput from class Truck then it says no name yet. I put super in the writeoutput but I'm not sure what i'm doing wrong. any help would be appreciated
public class Person {
private String name;
public Person()
{
name = "No Name Yet";
}
public Person(String initialName)
{
name = initialName;
}
public void setName(String newName)
{
newName = "tim";
name = newName;
}
public String getName()
{
return name;
}
public void writeOutput()
{
System.out.println("name: " + name);
}
public boolean hasSameName(Person otherPerson)
{
return this.name.equalsIgnoreCase(otherPerson.name);
}
}
public class Vehicle extends Person {
String manufactName;
int engine;
public Vehicle()
{
manufactName = "no name yet";
engine = 0;
}
public Vehicle(String initialManufactName, int initialEngine)
{
manufactName = initialManufactName;
engine = initialEngine;
}
public void setManufactName(String newManufactName)
{
manufactName = newManufactName;
}
public void setEngine(int newEngine)
{
engine = newEngine;
}
public String getManufactName()
{
return manufactName;
}
public double getEngine()
{
return engine;
}
public void writeOutput()
{
super.writeOutput();
System.out.println("Manufacturer: " + manufactName);
System.out.println("Engine Cyclinders: " + engine);
}
public boolean hasSameManufactName(Vehicle otherVehicle)
{
return this.manufactName.equalsIgnoreCase(otherVehicle.manufactName);
}
public boolean hasSameEngine(Vehicle otherVehicle)
{
return this.engine == (otherVehicle.engine);
}
}
public class Truck extends Vehicle {
private double load;
private double towing;
public Truck()
{
load = 0;
towing = 0;
}
public Truck(double initialLoad, double initialTowing)
{
load = initialLoad;
towing = initialTowing;
}
public void setLoad(double newLoad)
{
load = newLoad;
}
public void setTowing(double newTowing)
{
towing = newTowing;
}
public double getLoad()
{
return load;
}
public double getTowing()
{
return towing;
}
public void writeOutput()
{
super.writeOutput();
System.out.println("Load: " + load);
System.out.println("Towing: " + towing);
}
public boolean hasSameLoad(Truck otherTruck)
{
return this.load == (otherTruck.load);
}
public boolean hasSameTowing(Truck otherTruck)
{
return this.towing == (otherTruck.towing);
}
}
public class Driver extends Truck{
public static void main(String[] args){
Person p = new Person();
Vehicle v = new Vehicle();
Truck t = new Truck();
t.writeOutput();
}
}

You don't need to create an instance of each class in the hierachy.
Try:
Truck t = new Truck();
t.setName("tim");
t.writeOutput();
Also, your design is odd: Vehicles wouldn't normally subclass a Person, and a Driver wouldn't normally subclass a Truck. A Driver would be a subclass of a Person. A Truck would be a subclass of a vehicle and a Vehicle would "have" a Driver.

Related

Unchecked method 'sort(List<T>)' invocation with Generics in Java 11

I have three classes called League, Team and Player, League has an array with teams, and I have a method called showLeagueTable that shows team sorted by ranking(which is a method that exists in the Team class) and the class Team has an array of players.
League.java
public class League<T extends Team> {
public String name;
private List<T> teams = new ArrayList<>();
public League(String name) {
this.name = name;
}
public boolean add(T team) {
if (teams.contains(team)) {
return false;
}
return teams.add(team);
}
public void showLeagueTable() {
Collections.sort(this.teams);
for (T t: teams) {
System.out.println(t.getName() + ": " + t.ranking());
}
}
}
Team.java
public class Team<T extends Player> implements Comparable<Team<T>> {
private String name;
int played = 0;
int won = 0;
int lost = 0;
int tied = 0;
private ArrayList<T> members = new ArrayList<>();
public Team(String name) {
this.name = name;
}
public String getName() {
return name;
}
public boolean addPlayer(T player) {
if (members.contains(player)) {
System.out.println(player.getName() + " is already on this team");
return false;
} else {
members.add(player);
System.out.println(player.getName() + " picked for team " + this.name);
return true;
}
}
public int numPlayers() {
return this.members.size();
}
public void matchResult(Team<T> opponent, int ourScore, int theirScore) {
String message;
if (ourScore > theirScore) {
won++;
message = " beat ";
} else if (ourScore == theirScore) {
tied++;
message = " drew with ";
} else {
lost++;
message = " lost to ";
}
played++;
if (opponent != null) {
System.out.println(this.getName() + message + opponent.getName());
opponent.matchResult(null, theirScore, ourScore);
}
}
public int ranking() {
return (won * 2) + tied;
}
#Override
public int compareTo(Team<T> team) {
return Integer.compare(team.ranking(), this.ranking());
}
}
Player.java
public class Player {
private String name;
public Player(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
but in the line Collections.sort(this.teams); from the class League, I got the warning Unchecked method 'sort(List<T>)' invocation, I have read that maybe the problems is that I haven't implemented the Comparable interface in my class Team with a Type, but I did it you can see it at the line:
public class Team<T extends Player> implements Comparable<Team<T>>
can someone help me, please, thanks!
here a fully workable code for you specific issue :
it removes warning and errors.
phase 1 : remove comparable behaviour
phase 2 : try to compile without warning
phase 3 : try with a real new allocation : imporant, because, if you do not do anything new, T generics are not used, and you can not detect errors.
phase 4 : adding comparable behaviour.
final source [ see A) B) and C) mistakes in source]
League.java
package test;
import java.util.ArrayList;
import java.util.List;
import test.Team;
import java.util.Collections;
// -- A) missing precision about Team , Team based on Player
// -- public class League<T extends Team>
public class League<T extends Team<E>, E extends Player>
{
public String name;
// B) ! miss <T> after new :
// private List<T> teams = new ArrayList<>();
private List<T> teams = new ArrayList<T>();
public League(String name) {
this.name = name;
}
public boolean add(T team) {
if (teams.contains(team)) {
return false;
}
return teams.add(team);
}
public void showLeagueTable() {
Collections.sort(this.teams);
for (T t: teams) {
System.out.println(t.getName() + ": " + t.ranking());
}
}
}
Team.java
package test;
import java.util.ArrayList;
public class Team<T extends Player> implements Comparable<Team<T>> {
private String name;
int played = 0;
int won = 0;
int lost = 0;
int tied = 0;
// C) !! MISS <T> after new ArrayList
// private ArrayList<T> members = new ArrayList<>();
private ArrayList<T> members = new ArrayList<T>();
#Override
public int compareTo(Team<T> team) {
return Integer.compare(team.ranking(), this.ranking());
}
public Team(String name) {
this.name = name;
}
public String getName() {
return name;
}
public boolean addPlayer(T player) {
if (members.contains(player)) {
System.out.println(player.getName() + " is already on this team");
return false;
} else {
members.add(player);
System.out.println(player.getName() + " picked for team " + this.name);
return true;
}
}
public int numPlayers() {
return this.members.size();
}
public void showPlayers() {
for (T element : members) {
System.out.println(element.getName());
}
}
public void matchResult(Team<T> opponent, int ourScore, int theirScore) {
String message;
if (ourScore > theirScore) {
won++;
message = " beat ";
} else if (ourScore == theirScore) {
tied++;
message = " drew with ";
} else {
lost++;
message = " lost to ";
}
played++;
if (opponent != null) {
System.out.println(this.getName() + message + opponent.getName());
opponent.matchResult(null, theirScore, ourScore);
}
}
public int ranking() {
return (won * 2) + tied;
}
}
Player.java
package test;
public class Player {
private String name;
public Player(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
Test.java
package test;
import test.League;
import test.Team;
import test.Player;
public class Test {
Player p1;
Player p2;
Player p3;
Player p4;
Player p5;
Player p6;
Player p7;
Player p8;
Player p9;
Team<Player> t1;
Team<Player> t2;
Team<Player> t3;
Team<Player> t4;
public void test() {
System.out.println("RUNNING TEST");
p1=new Player("Mike");
p2=new Player("John");
p3=new Player("Jenny");
p4=new Player("Sunny");
p5=new Player("Mike");
p6=new Player("Jeremy");
p7=new Player("Gwen");
p8=new Player("Hector");
p9=new Player("Henry");
t1=new Team<Player>("Team1");
t2=new Team<Player>("Team2");
t3=new Team<Player>("Team3");
t4=new Team<Player>("Team4");
t1.addPlayer(p1);
t1.addPlayer(p2);
t2.addPlayer(p3);
t2.addPlayer(p4);
t2.addPlayer(p5);
t3.addPlayer(p6);
t3.addPlayer(p7);
t4.addPlayer(p8);
t4.addPlayer(p9);
// test show players
t1.showPlayers();
System.out.println("------------");
System.out.println("num players in team is "+t1.numPlayers());
System.out.println("---adding team in league ---------");
League<Team<Player>, Player> g1=new League<Team<Player>, Player>("League 1");
g1.add(t1);
g1.add(t2);
League<Team<Player>, Player> g2=new League<Team<Player>, Player>("League 2");
g2.add(t3);
g2.add(t4);
System.out.println("---settings results ---------");
t1.matchResult(t2, 2, 8);
t2.matchResult(t1, 1, 7);
t3.matchResult(t4, 4, 5);
t3.matchResult(t4, 4, 0);
System.out.println("-----League 1---------");
g1.showLeagueTable();
System.out.println("-----League 2---------");
g2.showLeagueTable();
}
}
Main.java
import test.Test;
public class Main
{
public static void main(String[] args) {
System.out.println("running MAIn");
Test test = new Test();
test.test();
}
}

java method which can be changed

I'm a little unclear on how to make this method. it has to make a method called setCapacity that later can override. I have to "Modify the class Vehicle, the base class, to include a method called setCapacity which
allows the engine capacity to be changed"
class Vehicle {
void setCapactiy1 () {
int setCapactity == 0;
}
}
int capacity;
String make;
Vehicle(int theCapacity, String theMake) {
capacity = theCapacity;
make = theMake;
}
void print() {
System.out.println("Vehicle Info:");
System.out.println(" capacity = " + capacity + "cc" );
System.out.println(" make = " + make );
}
}
class Car extends Vehicle {
public String type;
public String model;
public Car(int theCapacity, String theMake, String theType, String theModel) {
super(theCapacity, theMake);
type = theType;
model = theModel;
}
#Override
public void print() {
super.print();
System.out.println(" type = " + type);
System.out.println(" model = " + model);
}
#Override
}
class Task2 {
public static void main(String[] args) {
Car car1 = new Car(1200,"Holden","sedan","Barina");
Car car2 = new Car(1500,"Mazda","sedan","323");
car1.print();
car2.print();
}
}
Seems like a basic setter... It should go like
public void setCapacity(int newCapacity) {
this.capacity = newCapacity;
}

Calling Parameters from parent constructor

How do you call the parents constructor and give the parent constructor a parameter of 50? I need to make a constructor for HoldenDB which as no formal parameter and calls its parents constructor.
I have started by extending HoldeDB to VechicleDB, however, I'm unsure how to proceed for there.
If someone could help me that would be much appreciated.
import java.util.ArrayList;
class Vehicle {
int capacity;
String make;
void setCapacity(int setCapacity) {
this.capacity = setCapacity;
System.out.println("New Capacity = " + setCapacity);
}
Vehicle(int theCapacity, String theMake) {
capacity = theCapacity;
make = theMake;
}
void print() {
System.out.println("Vehicle Info:");
System.out.println(" capacity = " + capacity + "cc" );
System.out.println(" make = " + make );
}
}
class Car extends Vehicle {
public String type;
public String model;
public Car(int theCapacity, String theMake, String theType, String theModel) {
super(theCapacity, theMake);
type = theType;
model = theModel;
}
#Override
public void print() {
super.print();
System.out.println(" type = " + type);
System.out.println(" model = " + model);
}
#Override
public void setCapacity(int setCapacity) {
System.out.println("Cannot change capacity of a car");
}
}
class VehicleDB {
ArrayList<Vehicle> db = new ArrayList<Vehicle>();
void addVehicle(Vehicle c){
db.add(c);
}
void print(){
System.out.println("=== Vehicle Data Base ===");
for(Vehicle v: db){
v.print();
}
}
}
class HoldenDB extends VehicleDB {
void addCar(Vehicle c){
db.add(c);
}
}
class Task5 {
public static void main (String[]args){
HoldenDB db = new HoldenDB ();
db.addCar(1200,"sedan","Barina");
db.addCar(3800,"wagon","Commodore");
db.print();
}
}
public class VehicleDB {
private int n;
public VehicleDB(int n) {
this.n = n;
}
}
public class HoldenDB extends VehicleDB {
public HoldenDB() {
super(50);
}
}

Cant access classes through Object Java RPG character builder

Im trying to create a small character builder, using inheritance. i have CreateCharacter CharacterRace then a Dwarf class. i made a variable with type CharacterRace in CreateCharacter and a variable with type Dwarf in CharacterRace. i have an object of CreateCharacter in my main method demo and its not letting me call the methods from the Dwarf class, to make a dwarf character. im thinking ineed to pass a dwarf object in characterRace? im just not sure how. heres my code: (its a bit long my apologies)
package characterCreation;
public class CreateCharacter {
private CharacterClass characterClass;
private CharacterRace characterRace;
private Name name;
public CreateCharacter(String characterName,CharacterClass characterClass,CharacterRace characterRace) {
this.name = new Name(characterName);
this.characterClass = characterClass;
this.characterRace = characterRace;
}
public CreateCharacter(){
}
public CharacterClass getCharacterClass() {
return characterClass;
}
public void setCharacterClass(CharacterClass characterClass) {
this.characterClass = characterClass;
}
public CharacterRace getCharacterRace() {
return characterRace;
}
public void setCharacterRace(CharacterRace characterRace) {
this.characterRace = characterRace;
}
public Name getName(){
return name;
}
public void setName(Name name){
this.name = name;
}
#Override
public String toString() {
return "CreateCharacter [name=" + name + ", characterRace=" + characterRace + ", characterClass="
+ characterClass + "]";
}
}
package characterCreation;
public class CharacterRace {
protected String raceName;
protected double mana;
protected double hp;
private Dwarf dwarf;
public CharacterRace(String raceName,double mana, double hp) {
this.raceName = raceName;
this.mana = mana;
this.hp = hp;
}
public CharacterRace(){
}
public String getRaceName() {
return raceName;
}
public Dwarf getDwarf() {
return dwarf;
}
public void setDwarf(Dwarf dwarf) {
this.dwarf = dwarf;
}
public double getMana() {
return mana;
}
public double getHp() {
return hp;
}
#Override
public String toString() {
return "CharacterRace [dwarf=" + dwarf + "]";
}
}
package characterCreation;
public class Dwarf extends CharacterRace {
public Dwarf(String raceName,double mana, double hp) {
super(raceName,mana,hp);
}
public double getMana() {
mana = 5;
return mana;
}
public double getHp() {
hp = 10;
return hp;
}
public String getRaceName(){
return raceName = "Dwarf";
}
#Override
public String toString() {
return "Dwarf [mana=" + mana + ", hp=" + hp + ", getRaceName()=" + getRaceName() + "]";
}
}
package characterCreation;
import java.util.Scanner;
public class CharacterDemo {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
CreateCharacter create = new CreateCharacter();
System.out.println("Choose your Race: ");
String userRace = input.next();
create.setName(new Name("Daxel"));
//create.setCharacterRace(race);
System.out.println(create.getName());
//Dwarf dwarf = new Dwarf();
System.out.println(create.getCharacterRace().getDwarf().getRaceName());
//System.out.println(create.getCharacterRace().setDwarf(new Dwarf("dwarf",10,5)));
}
}
You have to call setCharacterRace() on create; then call setDwarf() on the characterRace; otherwise create.getCharacterRace() would be null and create.getCharacterRace().getDwarf() would throw NullPointerException.
I don't understand the logic behind your code, but try the code below:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
CreateCharacter create = new CreateCharacter();
System.out.println("Choose your Race: ");
String userRace = input.next();
create.setName(new Name("Daxel"));
//***********new code starts******
CharacterRace myRace = new CharacterRace(userRace, 20, 9);
myRace.setDwarf(new Dwarf("dwarf",10,5));
create.setCharacterRace(myRace);
//***********new code ends********
//create.setCharacterRace(race);
System.out.println(create.getName());
//Dwarf dwarf = new Dwarf();
System.out.println(create.getCharacterRace().getDwarf().getRaceName());
//System.out.println(create.getCharacterRace().setDwarf(new Dwarf("dwarf",10,5)));
}

"Color cannot be resolved to a type"?

import java.awp.*;
public class Reindeer
{
private String name;
private boolean canFly;
private Color noseColor;
private int antlers;
public Reindeer()
{
}
public Reindeer(String nameIn, boolean canFlyIn, Color noseColorIn, int antlersIn)
{
name = nameIn;
canFly = canFlyIn;
noseColor = noseColorIn;
antlers = antlersIn;
}
public String getName()
{
return name;
}
public boolean getCanFly()
{
return canFly;
}
public Color getNoseColor()
{
return noseColor;
}
public int getAntlers()
{
return antlers;
}
public void setCanFly(boolean canFlyIn)
{
canFly = canFlyIn;
}
public void setNoseColor(Color noseColorIn)
{
noseColor = noseColorIn;
}
public void setAntlers(int antlersIn)
{
antlers = antlersIn;
}
public String toString()
{
return "The Reindeer's name is " + name + " and it has a " + noseColor + " nose.";
}
}
Unsure of what to do here, would be great if I had some help. I know I have to import something but I had forgotten what it is and I believe it is something along the lines of 'import java.awp.*;' Thanks!
I think that's a typo. The java.* top-level packages are restricted, so there are only so many and they are all known. awp is probably supposed to be awt.

Categories

Resources