I have 2 classes that extend for each other. I have a mutator method that is calling to the class Address.Java to get the city and state from the class. However the Bank.java class is giving me problems. the specific method giving me problems is
public void setBankAddress(String aCity, String aState)(code is below) I dont know if this makes sense
{
}
Bank.Java
public class Bank
{
private String bankName;
private int bankID;
private Address bankAddress;
public Bank()
{
bankName = "?";
bankID = 0;
bankAddress = new Address();
}
public String getBankName()
{
return bankName;
}
public int getBankID()
{
return bankID;
}
public Address getBankAddress()
{
return bankAddress;
}
public void setBankName(String aBankName)
{
bankName = aBankName;
}
public void setBankID(int aBankID)
{
bankID = aBankID;
}
public void setBankAddress(String aCity, String aState)
{
}
public String toString()
{
return "\nBank name:\t\t" + bankName + "\nBank ID:\t\t" + bankID + "\nBank Address:\t\t" + bankAddress + "\n\n";
}
}
Address. Java
public class Address
{
private String city;
private String state;
public Address()
{
city = "?";
state = "?";
}
public String getCity()
{
return city;
}
public String getState()
{
return state;
}
public void setCity(String aCity)
{
city = aCity;
}
public void setState(String aState)
{
state = aState;
}
public String toString()
{
return city + "," + state;
}
}
Do you just not know how to call methods on objects?
public void setBankAddress(String aCity, String aState)
{
bankAddress.setCity(aCity);
bankAddress.setState(aState);
}
bankAddress is already initialized in the constructor so you can safely call these methods.
However, it's usually good practice to do that outside such methods and pass the whole object itself
Address someAddress = new Address();
someAddress.setCity(aCity);
someAddress.setState(aState);
Bank bank = new Bank();
bank.setBankAddress(someAddress);
...
public void setBankAddress(Address aBankAddress) {
bankAddress = aBankAddress;
}
Related
I have an assignment and my superclass default values always override the values I pass in the Test main method. In the debugger, i see the passing of the productNumber(1234) and productTitle("Daughter"), but then it's overridden with the default values. Any thoughts, i keep making minor changes, checking for changes, still the same results.
Product Superclass
public abstract class Product {
private int productNumber;
private String productTitle;
//Two constructors required
public Product(){
productNumber = 0;
productTitle = "";
}
public Product(int productNumber, String productTitle) {
this.productNumber = productNumber;
this.productTitle = productTitle;
}
public void setProductNumber(int productNumber) {
this.productNumber = productNumber;
}
public int getProductNumber() {
return productNumber;
}
public void setProductTitle(String productTitle) {
this.productTitle = productTitle;
}
public String getProductTitle() {
return productTitle;
}
//Override toString() required
#Override
public String toString() {
return productNumber + " " + productTitle;
}
// Required Product class declares abstract method with this signature: public String getDisplayText()
public abstract String getDisplayText();
//Override equals() required
#Override
public boolean equals(Object object) {
if (object instanceof Product) {
Product product2 = (Product) object;
if (productNumber == (product2.getProductNumber()) &&
productTitle.equals(product2.getProductTitle())){
return true;
}
}
return false;
}
}
Music Subclass extends Product Superclass
public class Music extends Product {
private String artist;
private String style;
private String medium;
public Music() {
super();
artist = "";
style = "";
medium = "";
}
public Music(int productNumber, String productTitle, String artist, String style, String medium) {
super();
this.artist = artist;
this.style = style;
this.medium = medium;
}
public String getArtist() {
return artist;
}
public void setArtist(String artist) {
this.artist = artist;
}
public String getStyle() {
return style;
}
public void setStyle(String style) {
this.style = style;
}
public String getMedium() {
return medium;
}
public void setMedium(String medium) {
this.medium = medium;
}
#Override
public String getDisplayText() {
return super.toString() + " by " + artist + " " + style + " " + medium;
}
#Override
public boolean equals(Object object){
if (object instanceof Music){
Music m = (Music) object;
if (artist.equals(m.getArtist()) &&
style.equals(m.getStyle()) &&
medium.equals(m.getMedium())){
return true;
}
}
return false;
}
}
Print String
public class Test {
public static void main(String[] args) {
// Expected result: 1234 Daughter by Pearljam Alternative online
Music music1 = new Music(1234,"Daughter", "Pearljam","Alternative","online");
System.out.println(music1.getDisplayText());
}
}
you are not passing values from subclass to your parentclass
instead of super() you need to do below -
super(productNumber,productTitle);
update needed in your code :-
public Music(int productNumber, String productTitle, String artist, String style, String medium) {
super(productNumber,productTitle);
this.artist = artist;
this.style = style;
this.medium = medium;
}
You need to pass productNumber and productTitle in the super(..., ...) call inside the Music constructor up to the parent class.
You need to invoke
super(productNumber, productTitle)
inside the Music constructor to pass the parameters to its parent.
the student and teacher class inherited from person class, because inheritance is not flexible the object will either be one of the two. to solve this i added an abstract class Role and make association between person and role and student and teacher inherits from Role class so that an instance can has a role of both student and teacher.The question is to make instance of the Teacher class can also be a student or a student also teaches how to test in the main class?
public class Person {
private int ssn;
private String name;
private List<PersonRole> roles;
public Person(int ssn, String name) {
this.ssn = ssn;
this.name = name;
roles = new ArrayList<PersonRole>();
}
public int getSsn() {
return ssn;
}
public String getName() {
return name;
}
}
/////////////// PersonRole /////////////////
public abstract class PersonRole {
private List<PersonRole> learning;
private List<PersonRole> teaching;
public PersonRole(List<PersonRole> learning, List<PersonRole> teaching) {
this.learning = learning;
this.teaching = teaching;
}
public List<PersonRole> getLearning() {
return learning;
}
public List<PersonRole> getTeaching() {
return teaching;
}
public void addAsStudent(PersonRole p) {
learning.add(p);
}
public void addAsTeacher(PersonRole p) {
learning.add(p);
}
}
///////////// Teacher //////////////
public class Teacher extends PersonRole {
private String faultyName;
private int yearsOfExperence;
public Teacher(String faultyName, int yearsOfExperence) {
super();
this.faultyName = faultyName;
this.yearsOfExperence = yearsOfExperence;
}
public String getFaultyName() {
return faultyName;
}
public int getYearsOfExperence() {
return yearsOfExperence;
}
}
/////////// Student ////////////////
public class Student extends PersonRole {
private String collegeName;
private String major;
public Student(String collegeName, String major) {
super();
this.collegeName = collegeName;
this.major = major;
}
public String getCollegeName() {
return collegeName;
}
public String getMajor() {
return major;
}
}
////// Main ///////////////
public class Main {
public static void main(String[] args) {
PersonRole p1 = new Student("univ of Michigan", "CS");
PersonRole p3 = new Student("Iowa state univ", "managemnt");
PersonRole p2 = new Teacher("computer science", 3);
PersonRole p4 = new Teacher("Management", 2);
Person person = new Person(1042327867, "Mike Rose");
person.addRole(p1);
person.addRole(p2);
person.addRole(p3);
person.addRole(p4);
for (PersonRole c: roles) {
System.out.println(c);
}
}
}
I would test it exactly as you would but the hierarchy of inheritance looks good. Why not give PersonRole a constructor and attributes so that the children can use super() in their constructors and reuse some code?
Try this to print your roles:
List<PersonRole> roles = person.getRole();
for(PersonRole role: roles) {
System.out.println(role);
}
Then add a toString method in the Student and Teacher classes:
public String toString() {
return "Student {" + "collegeName=" + collegeName + ", major=" + major + '}';
}
public String toString() {
return "Teacher {" + "faultyName=" + faultyName + ", yearsOfExperence=" + yearsOfExperence + '}';
}
The result will be this:
Student {collegeName=maharashi univ, major=compro}
Teacher {faultyName=computer science, yearsOfExperence=3}
Student {collegeName=Iowa state univ, major=managemnt}
Teacher {faultyName=Management, yearsOfExperence=2}
I am just wondering why my getTotal_Score() is null in my model class but I can retrieve it successfully from the database...I have tried both capital Integer and just a normal int, but that doesn't work...I have also tried Long type but I still get a null variable..The other thing that is puzzling me too is that when I use int type instead of long, it doesn't work.... Can you retrieve the numbers as int simple by doing:
int score = (int) snapshop.child("Glen Family Medical Centre").child("Total Score");
I can only get the above to work with long type...
public class Information { // variables have to match in firebase database or it will show null
private String Address;
private String Name;
private String Phone_No;
private String Suburb;
private String State;
private String Postcode;
private String Doctor;
private int Total_Score;
#SuppressWarnings("unused")
public Information() {
}
#SuppressWarnings("unused")
public Information(String address, String name, String phone_No, String suburb, String state, String postcode, String doctor, int total_score) {
Address = address;
Name = name;
Phone_No = phone_No;
Suburb = suburb;
State = state;
Postcode = postcode;
Doctor = doctor;
Total_Score = total_score;
}
public int getTotal_Score() {
return Total_Score;
}
public void setTotal_Score(int total_Score) {
Total_Score = total_Score;
}
public String getAddress() {
return Address;
}
#SuppressWarnings("unused")
public void setAddress(String address) {
Address = address;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getPhone_No() {
return Phone_No;
}
#SuppressWarnings("unused")
public void setPhone_No(String phone_No) {
Phone_No = phone_No;
}
public String getSuburb() {
return Suburb;
}
#SuppressWarnings("unused")
public void setSuburb(String suburb) {
Suburb = suburb;
}
public String getState() {
return State;
}
#SuppressWarnings("unused")
public void setState(String state) {
State = state;
}
public String getPostcode() {
return Postcode;
}
#SuppressWarnings("unused")
public void setPostcode(String postcode) {
Postcode = postcode;
}
public String getDoctor() {
return Doctor;
}
#SuppressWarnings("unused")
public void setDoctor(String doctor) {
Doctor = doctor;
}
}
In my other class, I have used:
Information info = snapshot.getValue(Information.class);
assert info != null;
String txt = "Medical Clinic: " + info.getName() + "Total Score: " + info.getTotal_Score();
list.add(txt);
So i have 2 classes, and in the class race i have a method ( public Athlete getAthlete(int codAthlete) ) that
should return the object corresponding to the Athlete with the code passed by parameter, but i am not sure how to
implement it. Can someone give me a hand?
public class Athlete {
private int codAthlete;
private String name;
public Athlete(int codAthlete){
this.codAthlete = codAthlete;
}
public int getCodAthlete() {
return this.codAthlete;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public String getInformation() {
return "Code: " + this.codAthlete +
" Name " + this.name;
}
}
.
public class Race {
private String idRace;
private Set<Athlete> athletes;
public Race(String idRace) {
athletes = new HashSet<>();
this.idRace = idRace;
}
public String getIdRace () {
return this.idRace;
}
public Athlete getAthlete(int codAthlete){
for(Athlete a: Athlete){
if(a.getCodAthlete() == codAthlete)
a.getInformation();
}
return (????);
// Returns the object corresponding to the Athlete with the code passed by parameter.
}
}
public class ParkedCar {
private String make;
private String model;
private String color;
private String licenseNum;
public ParkedCar(String make, String model, String color, String licenseNum) {
this.make = make;
this.model = model;
this.color = color;
this.licenseNum = licenseNum;
}
public void setMake(String ma) {
make = ma;
}
public void setModel(String mo) {
model = mo;
}
public void setColor(String c) {
color = c;
}
public void setLicenseNum(String ln) {
licenseNum = ln;
}
public String getMake() {
return make;
}
public String getModel() {
return model;
}
public String getColor() {
return color;
}
public String getLicenseNum() {
return licenseNum;
}
}
public class ParkingMeter {
private ParkedCar parkedcar;
private int timePurchased;
private int timeParked;
public ParkingMeter(ParkedCar parkedcar, int timePurchased, int timeParked) {
this.parkedcar = parkedcar;
this.timePurchased = timePurchased;
this.timeParked = timeParked;
}
/*public ParkingMeter (ParkedCar parkedcar) {
this.parkedcar = null;
}*/
public void setTimePurchased(int timePurchased) {
this.timePurchased = timePurchased;
}
public int getTimePurchased() {
return timePurchased;
}
public void setTimeParked(int timeParked) {
this.timeParked = timeParked;
}
public int getTimeParked() {
return timeParked;
}
public int TimeExpired() {
if (timeParked > timePurchased)
return timeParked - timePurchased;
else
return 0;
}
public String toString() {
return "Make: " + parkedcar.getMake() + "\nModel: " + parkedcar.getModel() + "\nColor: " + parkedcar.getColor() + "\nLicense Number: " + parkedcar.getLicenseNum();
}
}
public class ParkingTicket {
private ParkingMeter parkingmeter;
public ParkingTicket(ParkingMeter parkingmeter) {
this.parkingmeter = parkingmeter;
}
public int TicketCost() {
if (parkingmeter.getTimeParked() > parkingmeter.getTimePurchased()) {
if (parkingmeter.getTimeParked() <= 60)
return 25;
else
return 25 + (10*(parkingmeter.TimeExpired())/60);
}
else
return 0;
}
}
public class PoliceOfficer {
private String OfficerName;
private int OfficerNum;
private ParkingMeter pm;
private ParkingTicket pt;
public PoliceOfficer(ParkingTicket pt, String OfficerName, int OfficerNum) {
this.OfficerName = OfficerName;
this.OfficerNum = OfficerNum;
}
public void setOfficerName(String OfficerName) {
this.OfficerName = OfficerName;
}
public void setOfficerNum(int OfficerNum) {
this.OfficerNum = OfficerNum;
}
public String getOfficerName() {
return OfficerName;
}
public int getOfficerNum() {
return OfficerNum;
}
public boolean isExpired() {
if (pm.getTimeParked() > pm.getTimePurchased())
return true;
else
return false;
}
public String toString() {
return "Officer Name: " + OfficerName + "\nOfficer Number: " + OfficerNum + "\n" + "\nFine: " + pt.TicketCost();
}
}
public class ParkingTicketDemo {
public static void main(String[] args) {
ParkedCar pc = new ParkedCar("Toyota", "Camry", "Blue", "BXZ 152");
System.out.println(pc);
ParkingMeter pm = new ParkingMeter(pc, 60, 120);
ParkingTicket pt = new ParkingTicket(pm);
PoliceOfficer po = new PoliceOfficer(pt, "Roger", 337);
System.out.println(po);
}
}
I have been trying to create a program to create and issue a parking ticket and have run into the problem where it compiles, but when it runs it gives out the error message Exception in thread "main" java.lang.NullPointerException. I am a fairly new programmer and this is the first time I have encountered the problem so I have yet fully understand it and cannot seem to fix it. i have tried reading other things online, but just do not understand I would love a simple explaination to my problem.
The NPE happens because of these two lines:
PoliceOfficer po = new PoliceOfficer(pt, "Roger", 337);
System.out.println(po);
In your constructor for PoliceOfficer, you don't do anything with the ParkingTicket instance pt.
public PoliceOfficer(ParkingTicket pt /* not set anywhere */, String OfficerName, int OfficerNum) {
this.OfficerName = OfficerName;
this.OfficerNum = OfficerNum;
}
The fields ParkingMeter pm and ParkingTicket pt remain null since you haven't initialized them.
Then you try to print the object: System.out.println(po); What this does is call toString() on po, it is equivalent to this:
System.out.println(po.toString());
Now because your toString()
public String toString() {
return "Officer Name: " + OfficerName + "\nOfficer Number: " + OfficerNum + "\n" + "\nFine: " + pt.TicketCost();
}
uses the pt, it creates a NullPointerException, since pt is null.
Since you are already passing a ParkingTicket instance into the constructor for PoliceOfficer, use that instance to assign its member variable pt.