Please help me find errors from this code. I'm still new and I don't know if this is correct or not.
I do have one error.
This is the error:
constructor Person in class Person cannot be applied to given types;
super();
^
required: String,String,String
found: no arguments
reason: actual and formal argument lists differ in length
This is my code:
import java.util.*;
public class Person {
//Data fields
private String lastName;
private String middleInitial;
private String firstName;
//Constructors
public Person(String lastName, String middleInitial, String firstName) {
this.lastName = lastName;
this.middleInitial = middleInitial;
this.lastName = lastName;
}
//Accessor methods
public String getlastName() {
return lastName;
}
public String getmiddleInitial() {
return middleInitial;
}
public String getfirstName() {
return firstName;
}
//Mutator methods
public void setlastName(String lastName) {
lastName = lastName;
}
public void setmiddleInitial(String middleInitial) {
middleInitial = middleInitial;
}
public void setfirstName(String firstName) {
firstName = firstName;
}
public String getName() {
String studentName = this.lastName + ", " + this.firstName +
this.middleInitial + ".";
return studentName;
}
} //end Person class
class Address {
//Data fields
private String streetName;
private int zipCode;
private String state;
private String country;
//Constructors
public Address(String streetName, int zipCode, String state,
String country) {
this.streetName = streetName;
this.zipCode = zipCode;
this.state = state;
this.country = country;
}
//Accessor methods
public String getstreetName() {
return streetName;
}
public int getzipCode() {
return zipCode;
}
public String getstate() {
return state;
}
public String getcountry() {
return country;
}
//Mutator methods
public void setstreetName(String streetName) {
streetName = streetName;
}
public void setzipCode(int zipCode) {
zipCode = zipCode;
//Integer.toString(zipCode);
}
public void setstate(String state) {
state = state;
}
public void setcountry(String country) {
country = country;
}
public String getAddress() {
String studentAddress = streetName + "\n" + state + ", " + country +
"\n" + zipCode;
return studentAddress;
}
} //end Address class
class Student extends Person {
private String dateOfBirth;
//Constructors
public Student (String studentName, String dateOfBirth) {
super();
dateOfBirth = dateOfBirth;
}
//Accessor methods
public String getdateOfBirth() {
return dateOfBirth;
}
//Mutator methods
public void setdateOfBirth() {
this.dateOfBirth = dateOfBirth;
}
public String toString() {
return ("Date of Birth: " + dateOfBirth);
}
} //end Student subclass
Edited: If I do so for both the Person and Address class. I can only have three-arg constructors. How can I call a one-arg constructor?
For example, I have
public Student (String firstName, String lastName, String middleInitial, String dateOfBirth) {
super(firstName, lastName, middleInitial); and
public Student (String streetName, String state, String country) {
super(streetName, state, country);
How can I get zipcode separately?
Class Person has a constructor, therefore the default no-arg constructor is not created for you. Therefore you can't call super() in Student's constructor, you have to call super(lastName, middleInitial, firstName);.
Or you could create a new Person no-arg constuctor.
Try this
In student class
public Student ( String lastName, String middleInitial, String firstName,String studentName, String dateOfBirth) {
super( lastName, middleInitial,firstName);
this.dateOfBirth = dateOfBirth;
}
Or
In Person class create no arg consructor. Eg:
public Person(){}
Person Class has a constructor with arguments. So default constructor will not be created. So you have to pass 3 String parameters in super(3 String parameters) or you have to create a constructor which does not take any parameter in person class.
Related
package week3;
import java.util.ArrayList;
public abstract class TaxPayer {
private final String TFN;
private String firstName;
private String lastName;
private double income;
private Address address(int streetNumber, String streetName, String suburb,
String city, String state, int postcode);
public TaxPayer(String TFN, String firstName, String lastName, double income,
Address address) {
this.TFN = TFN;
this.firstName = firstName;
this.lastName = lastName;
this.income = income;
this.address = address;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public double getIncome() {
return income;
}
public Address getAddress() {
return address;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setIncome(double income) {
this.income = income;
}
public void setAddress(Address i) {
}
public String getFullName() {
return getFirstName() + " " + getLastName();
}
#Override
public String toString() {
return TFN + " " + firstName + " " + lastName + " "
+ income;
}
public static double calcTax(double income) {
return 1;
}
public double calcTax() {
return 1;
}
public static void printArrayListToConsole(ArrayList<TaxPayer> Array) {
for (TaxPayer d : Array) {
System.out.println(d);
}
}
}
package week3;
public class Address {
int streetNumber;
String streetName;
String suburb;
String city;
String state;
int postcode;
public Address(int streetNumber, String streetName, String suburb,
String city, String state, int postcode) {
this.streetNumber = streetNumber;
this.streetName = streetName;
this.suburb = suburb;
this.city = city;
this.state = state;
this.postcode = postcode;
}
#Override
public String toString() {
return streetNumber + " " + streetName + " " + suburb + " " + city + " "
+ state + " " + postcode;
}
}
So, my problem is in creating the address attribute, which must be private. I think I've made the other class correctly yet it still isn't working. As well as this, the attribute isn't working with the fields I try to put in it (streetNumber, streetName, etc), it's coming up with the error 'missing method body, or declare abstract' but I'm unsure why. Any help is appreciated!
private Address address(int streetNumber, String streetName, String suburb,
String city, String state, int postcode);// this is a method not a attribute
Make private attribute of Address Class.
public abstract class TaxPayer {
.
.
.
private Address address; // this is attribute.
.
.
.
//One way is passing the all address attributes in constructor
public TaxPayer(String TFN, String firstName, String lastName, double income,int streetNumber, String streetName, String suburb,
String city, String state, int postcode) {
this.TFN = TFN;
this.firstName = firstName;
this.lastName = lastName;
this.income = income;
this.address = new Address(streetNumber, streetName, String suburb,city, state, postcode);
}
}
second option is use same constructor and make a Address objectin main class and pass into that constructor like :
public TaxPayer(String TFN, String firstName, String lastName, double income,Address address) {
this.TFN = TFN;
this.firstName = firstName;
this.lastName = lastName;
this.income = income;
this.address = address;
}
And while creating Tax Payer Object first create Address Object and pass it.
Address address= new Address(streetNumber, streetName, String suburb,city, state, postcode);
TaxPayer tp= new TaxPayer(TFN, firstName, lastName, income,address);
private Address address(int streetNumber, String streetName, String suburb,
String city, String state, int postcode);
You have to declaring a variable of Address type but this is not the way to declare a variable.
Replace the above code of TaxPayer class with just this
private Address address;
I have a problem passing params into a super constructor. I'm implementing Abstraction with Inheritance. I created three classes that extends the abstract class Employee. But when I created the objects of these three classes, only one class passes the arguments correctly to the super() constructor, while the rest will return null.
Here is the abstract class
public abstract class Employee {
private String firstName,lastName,SSN;
public Employee(String fName, String lName, String SSN){
this.SSN = SSN;
this.firstName = fName;
this.lastName = lName;
}
public void setFirstName(String fName){
this.firstName = firstName;
}
public void setLastName(String lName){
this.lastName = lName;
}
public void setSSN(String SSN){
this.SSN = SSN;
}
public String getSSN(){
return SSN;
}
public String getFirstName(){
return firstName;
}
public String getLastName(){
return lastName;
}
public abstract double getEarnings();
#Override
public String toString(){
return String.format("First Name: %-2s \n Last Name: %-2s \n Social Security Number: %-2s", getFirstName(), getLastName(),getSSN());
}
And here are the classes:
public class SalariedEmployee extends Employee {
private String firstName, lastName, SSN;
double weeklyPay;
public SalariedEmployee(String fName, String lName, String SSN, double wPay) {
super(fName, lName, SSN);
this.weeklyPay = wPay;
}
public void setWeeklyPay(double wPay) {
this.weeklyPay = wPay;
}
public double getWeeklyPay() {
return weeklyPay;
}
#Override
public double getEarnings() {
return getWeeklyPay();
}
#Override
public String toString() {
return String.format("%s \n Weekly Pay: %2s", super.toString(), getEarnings());
}
}
public class HourlyEmployee extends Employee{
private String firstName, lastName, SSN;
private double hourlyPay,wage;
private int hours;
public HourlyEmployee(String fName, String lName, String SSN, int hour, double hourlyPay ){
super(fName, lName, SSN);
this.hours = hour;
this.hourlyPay = hourlyPay;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getSSN() {
return SSN;
}
public void setSSN(String SSN) {
this.SSN = SSN;
}
public double getHourlyPay() {
return hourlyPay;
}
public void setHourlyPay(double hourlyPay) {
this.hourlyPay = hourlyPay;
}
public double getWage() {
return wage;
}
public void setWage(double wage) {
this.wage = wage;
}
public int getHours() {
return hours;
}
public void setHours(int hours) {
this.hours = hours;
}
#Override
public double getEarnings(){
double earnings;
if(hours> 40)
earnings = hourlyPay * 40 + ((hours-40) *hourlyPay *1.5);
else
earnings = hourlyPay * hours;
return earnings;
}
#Override
public String toString(){
return String.format("%s \n Hours worked: %s \n Salary: %s \n ", super.toString(), getHours(), getEarnings());
}
}
public class CommissionEmployee extends Employee {
private String firstName, lastName, SSN;
private double sales,commission;
public CommissionEmployee(String fName, String lName, String SSN, double sales, double commission){
super(fName, lName, SSN);
this.commission = commission;
this.sales = sales;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getSSN() {
return SSN;
}
public void setSSN(String SSN) {
this.SSN = SSN;
}
public double getSales() {
return sales;
}
public void setSales(double sales) {
this.sales = sales;
}
public double getCommission() {
double commissionRate;
if(commission >0 && commission<1)
return commission;
else
return 0;
}
public void setCommission(double commission) {
this.commission = commission;
}
#Override
public double getEarnings(){
return getCommission() * getSales();
}
#Override
public String toString(){
return String.format("%s \n Commission Rate: %s \n Salary: %s \n", super.toString(), getCommission(), getEarnings());
}
}
And Here is the TEST Class
public class EmployeeTest {
public static void main(String[] args) {
Employee salariedEmp,hourlyEmp,commissionEmp;
salariedEmp = new SalariedEmployee("Aminu", "Bishir", "11-22-33-33", 300.47);
System.out.println(printDetails(salariedEmp));
hourlyEmp = new HourlyEmployee("Musa","Sale","22-33-44-55",50,20.4);
System.out.println(printDetails(hourlyEmp));
commissionEmp = new CommissionEmployee("Muhammad","Ibrahim","33-44-55-66",20000,0.3);
System.out.print( printDetails(commissionEmp));
}
public static String printDetails(Employee emp){
return String.format("%s \n %s",emp.getClass(),emp.toString());
}
}
And this is the output I get whenever I run it:
class Abstraction.SalariedEmployee
First Name: Aminu
Last Name: Bishir
Social Security Number: 11-22-33-33
Weekly Pay: 300.47
class Abstraction.HourlyEmployee
First Name: null
Last Name: null
Social Security Number: null
Hours worked: 50
Salary: 1122.0
class Abstraction.CommissionEmployee
First Name: null
Last Name: null
Social Security Number: null
Commission Rate: 0.3
Salary: 6000.0
You should delete line
private String firstName, lastName, SSN;
In child classes CommissionEmployee, HourlyEmployee and SalariedEmployee. Because these properties are declared in your abstract class Employee, use it.
You have a problem with declaring variables multiple times. By declaring them in a super class, they also get declared in a subclass. When you declare the variables with the same name in a subclass, you can address the superclass' variable via super.variable. But I would recommend you to simply delete the variable declarations in your subclasses, that were already declared in the superclass.
However this is not the root of your problem. A similar issue occures if you override methods, in your case the subclass that prints correctly does not override the getters, which are used to read out the information stored in your variables. In the class that prints out correctly the getters are found in the superclass and hence the getters are addressing the superclass variables. In the other 2 classes the overridden getters are found, that are using the variables found in the subclasses. Since you set only the variables in the superclass, the subclass variables are always null and so is your result.
I would recommend you to delete the overriden getters and the variables in the subclasses. You don't need them.
Variables
private String firstName,lastName,SSN;
are already defined in your employee class, no need to repeat them. That's why of those nulls
Summary:
New to Java, tried looking through other posts but didn't find an answer. I'm learning inheritance and have an AddressBook class extended by a Runner class. When I write a program to test the inheritance I create a Runner object. If I get the first String parameter it returns fine but when I attempt to get the second String parameter it returns null.
Question:
Why is the second parameter returning null?
package Assignment_1;
//Begin Class Definition
public class AddressBook {
// Member variables
private String businessPhone;
private String cellPhone;
private String facebookId;
private String firstName;
private String homeAddress;
private String homePhone;
private String lastName;
private String middleName;
private String personalWebSite;
private String skypeId;
//Constructors
public AddressBook (String firstName, String middleName, String lastName, String homeAddress, String businessPhone, String homePhone, String cellPhone, String skypeId, String facebookId, String personalWebSite) {
this.firstName = firstName;
this.middleName = middleName;
this.lastName = lastName;
this.homeAddress = homeAddress;
this.businessPhone = businessPhone;
this.homePhone = homePhone;
this.cellPhone = cellPhone;
this.skypeId = skypeId;
this.facebookId = facebookId;
this.personalWebSite = personalWebSite;
}
public AddressBook (String firstName) {
this.firstName = firstName;
}
public AddressBook(String firstName, String middleName) {
this.firstName = firstName;
this.middleName = middleName;
}
public AddressBook (String firstName, String middleName, String lastName) {
this.firstName = firstName;
this.middleName = middleName;
this.lastName = lastName;
}
// Getters and setters
public String getFirstName() {
return firstName;
}
public String getMiddleName() {
return middleName;
}
public String getLastName() {
return lastName;
}
public String getHomeAddress() {
return homeAddress;
}
public String getBusinessPhone() {
return businessPhone;
}
public String getHomePhone() {
return homePhone;
}
public String getCellPhone() {
return cellPhone;
}
public String getSkypeId() {
return skypeId;
}
public String getFacebookId() {
return facebookId;
}
public String getPersonalWebsite() {
return personalWebSite;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setMiddleName(String middleName) {
this.middleName = middleName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setHomeAddress(String homeAddress) {
this.homeAddress = homeAddress;
}
public void setBusinessPhone(String businessPhone) {
this.businessPhone = businessPhone;
}
public void setHomePhone(String homePhone) {
this.homePhone = homePhone;
}
public void setCellPhone(String cellPhone) {
this.cellPhone = cellPhone;
}
public void setSkypeId(String skypeId) {
this.skypeId = skypeId;
}
public void setFacebookId(String facebookId) {
this.facebookId = facebookId;
}
public void setPersonalWebSite(String personalWebSite) {
this.personalWebSite = personalWebSite;
}
// Public methods
public static void compareNames(String name1, String name2) {
if(name1.equals(name2)) {
System.out.println(name1);
System.out.println(name2);
System.out.println("The names are the same.");
} else {
System.out.println(name1);
System.out.println(name2);
System.out.println("The names appear to be different.");
}
}
************************************************************
package Assignment_1;
public class BanffMarathonRunner extends AddressBook {
// Member variables
private int time;
private int years;
// Constructors
public BanffMarathonRunner(String firstName, String lastName, int min, int yr) {
super(firstName, lastName);
time = min;
years = yr;
}
// Getters and Setters
public int getTime() {
return time;
}
public void setTime(int time) {
this.time = time;
}
public int getYears() {
return years;
}
public void setYears(int years) {
this.years = years;
}
}
************************************************************
package Assignment_1;
import Assignment_1.BanffMarathonRunner;
public class TestBanffMarathonRunner {
public static void main(String[] args) {
BanffMarathonRunner r1 = new BanffMarathonRunner("Elena", "Brandon", 341, 1);
System.out.print(r1.getLastName());
}
}
}
Because lastName is null.
You are calling AddressBook(String firstName, String middleName)
and setting the middleName, not the lastName.
BanffMarathonRunner r1 = new BanffMarathonRunner("Elena", "Brandon", 341, 1);
calls:
// firstName = "Elena"
// lastName = "Brandon"
// min = 341
// yr = 1
public BanffMarathonRunner(String firstName, String lastName, int min, int yr) {
super(firstName, lastName);
// ...
}
which calls via super(...):
// firstName = "Elena"
// middleName = "Brandon" <-- here is your issue
public AddressBook(String firstName, String middleName) {
this.firstName = firstName;
this.middleName = middleName;
}
Brandon is set in AddressBook#middleName instead of AddressBook#lastName.
Your problem is in the BanffMarathonRunner.java:
in the constructor when you are calling the
super(firstName, lastName);
Actually by the call above the super class constructor with two parameter is being called, and that constructor is the one which set the middleName not the lastName.
I think you are confused because of the lastName variable name, which is passed to the constructor with two argument and that constructor use the second argument to set the middleName.
Good Luck.
Recently I am doing a coding exercises I need to make my project , and so far I am practicing it with the code below what I want to ask is that, is this a has a relationship? am I doing the right practice? look at my code, sorry for my bad english
public class Personal {
private String firstName;
private String middleInitial;
private String lastName;
private int age;
public Personal(String firstName,String middleInitial , String lastName , int age){
setFirstName(firstName);
setMiddleInitial(middleInitial);
setLastName(lastName);
setAge(age);
}
public void setFirstName(String firstName){
this.firstName = firstName;
}
public String getFirstName(){
return firstName;
}
public void setMiddleInitial(String middleInitial){
this.middleInitial = middleInitial;
}
public String getMiddleInitial(){
return middleInitial;
}
public void setLastName(String lastName){
this.lastName = lastName;
}
public String getLastName(){
return lastName;
}
public void setAge(int age){
this.age = age;
}
public int getAge(){
return age;
}
public String toString(){
return String.format("First Name: "+getFirstName()+"\nMiddle Initial: "+getMiddleInitial()+
"\nLast Name: "+getLastName()+"\nAge: "+getAge());
}
}
Contact Class
public class Contact {
private String address;
private String email;
private String contactNumber;
public Contact(String address,String contactNumber, String email){
setAddress(address);
setContactNumber(contactNumber);
setEmail(email);
}
public void setAddress(String address){
this.address = address;
}
public String getAddress(){
return address;
}
public void setEmail(String email){
this.email = email;
}
public String getEmail(){
return email;
}
public void setContactNumber(String contactNumber){
this.contactNumber = contactNumber;
}
public String getContactNumber(){
return contactNumber;
}
public String toString(){
return String.format("Address: "+getAddress()+"\nContact Number: "+getContactNumber()+
"\nEmail Address: "+getEmail());
}
}
Employee Class
public class Employee {
private Personal personal;
private Contact contact;
public Employee(Personal personal, Contact contact){
this.personal = personal;
this.contact = contact;
}
public void setFirstName(String firstName){
this.personal.setFirstName(firstName);
}
public String toString(){
return String.format(personal.toString()+contact.toString());
}
}
And the Test class
public class TestClass {
public static void main(String[] args){
Personal personalHerp = new Personal("John","M","Doe",18);
Contact contactHerp = new Contact("88 Herp Derp St U mad New york","724-15-70","fido.com");
Employee employeeHerp = new Employee(personalHerp,contactHerp);
System.out.println(employeeHerp);
}
}
Well, since Employee doesn't extend Personal it has a Personal and a Contact.
I guess you'd rather like Employee to be a Personal and thus it should look like this:
public class Employee extends Personal {
private Contact contact;
...
}
So to summarize:
is-a means a class/object extends another class or implements an interface, i.e. A is-a B if A extends B or A implements B
has-a means that a class/object has a variable of that type, like Contact contact in your Employee class, which means Employee has-a contact.
Yes, this is a "has-a" relationship (exactly as we discussed in your other question).
New to Java...
I have a name class that has:
private String firstName;
private String middleInitial;
private String lastName;
as its instance variables.
If I had certain data that had only firstName and lastName, no middleInitial, how would I make the constructor so that it took only 2 parameters instead of three?
You simply write a constructor with two parameters and a constructor with three
public YourClass(String firstName, String lastName) {
...
}
public YourClass(String firstName, String middleInitial, String lastName) {
...
}
Callers can then choose to use the appropriate constructor based on their needs.
Well, two options:
Just have a constructor with three parameters, and call it using null or the empty string for middleInitial
Overload the constructors, possibly calling one from the other.
As an example for the latter, using an empty string as the default middle initial:
public Person(String firstName, String middleInitial, String lastName)
{
this.firstName = firstName;
this.middleInitial = middleInitial;
this.lastName = lastName;
}
public Person(String firstName, String lastName)
{
this(firstName, "", lastName);
}
However, the compiler will need to know which one you're calling from the call site. So you can do:
new Person("Jon", "L", "Skeet");
or
new Person("Jon", "Skeet");
... but you can't do:
// Invalid
new Person(firstName, gotMiddleInitial ? middleInitial : ???, lastName);
and expect the compiler to decide to use the "two name" variant instead.
In Java, constructors can't have default arguments. Your only option here is to write two constructors. Fortunately, Java does allow you to call constructors from other constructors. You could do something like:
public class MyClass {
private String firstName;
private String middleInitial;
private String lastName;
public MyClass(String firstName, String middleInitial, String lastName) {
this.firstName = firstName;
this.middleInitial = middleInitial;
this.lastName = lastName;
}
public MyClass(String firstName, String lastName) {
this(firstName, "", lastName);
}
...
}
public Class Name{
private String first;
private String middle;
private String last;
public Name(String first, String middle, String last){
this.first = first;
this.middle = middle;
this.last = last;
}
public Name(String first, String last){
this.first = first;
this.last = last;
}
}
You could use two constructors:
public Person(String firstName, String lastName)
{
this(firstName, null, lastName);
}
public Person(String firstName, String middleInitial, String lastName)
{
this.firstName = firstName;
this.middleInitial = middleInitial;
this.lastName = = lastName;
}
Define 2 constructors, one with 2 parameters and one with 3 parameters.
You can write two constructors.
public Person( String firstName, String lastName, String middleName ) { ... }
public Person( String firstName, String lastName ) { ... }
public void myBike(String name, String color)
{
System.out.println("My bike's name is " + name + " and is " + color + ".");
}
public void myBike(String name, String color, float height)
{
System.out.println("My bike's name is " + name + " and is " + color + ".");
System.out.println("My bike is also " + height + " inches tall.");
}
Builder pattern...
class Name {
Builder builder;
public String getSurname() {
return builder.surname;
}
// getter...
public Name(Builder builder) {
this.builder = builder;
}
class Builder {
String surname = "";
String middleName = "";
String name = "";
Builder surname(String surname) {
this.surname = surname;
return this;
}
Builder middleName(String middleName) {
this.middleName = middleName;
return this;
}
Builder name(String name) {
this.name = name;
return this;
}
Name build() {
return new Name(this);
}
}
}