Test and sub-class - java

I want to implement a test to check my sub-class. How do I test that Customer class extends the Person class at run time?
Super Class
import java.util.Scanner;
public class Person
{
private String name;
private String address;
private String number;
public Person() //No Argument constructor//
{
name= "";
address= "";
number= "" ;
}
public Person(String num, String nam, String add) //Explicit value constructor//
{
number= num;
name= nam;
address= add;
}
public String getName() //Accessor method//
{
return name;
}
public void setName(String name )//Mutator method//
{
this.name= name;
}
public String getAddress()
{
return address;
}
public void setAddress(String address)
{
this.address= address;
}
public String getTelephoneNumber()
{
return number;
}
public void setNumber(String number)
{
this.number= number;
}
public String toString ()
{
return name + "\n" + address + "\n" + number;
}}
Sub Class
public class Customer extends Person
{
public Customer(String num, String nam, String add)
{
super (num, nam, add);
}
public boolean checkResponse( char response)
{
if (response == 'Y'){return true;}
return false;
}
public void display()
{
System.out.println(super.toString());
}
}

Related

What is my java code missing - won't output anything

When I run my code in NetBeans, it just says Build Successful, it does not include any output.
Can you please help me review my code to find out what it is missing?
I have tried everything that I know to do.
public class Lab04 {
public static void main(String[] args) {
class Person {
String name;
String address;
String phoneNumber;
public Person(String name, String address, String phoneNumber) {
this.name = name;
this.address = address;
this.phoneNumber = phoneNumber;
}
#Override
public String toString() {
return String.format("Name: "+name+"Address: "+address+"Phone Number: "+phoneNumber);
}
public static class Student extends Person {
String status;
public Student(String name, String address, String phoneNumber, String status) {
super(name, address, phoneNumber);
this.status = status;
}
#Override
public String toString() {
return String.format("Name: "+name+"Address: "+address+"Phone Number: "+phoneNumber+"Status: "+status);
}
}
public static class Instructor extends Person {
String rank;
public Instructor(String name, String address, String phoneNumber, String rank) {
super(name,address,phoneNumber);
this.rank = rank;
}
#Override
public String toString() {
return String.format("Name: "+name+"Address: "+address+"Phone Number: "+phoneNumber+"Rank: "+rank);
}
}
//Testing code - printing statements
public class TestingCode {
public static void main(String [] args) {
Person person = new Person("Peter", "111 Main St.", "2223333");
System.out.println(person);
person = new Student("Susan", "123 2nd Ave.", "3334444", "Sophomore");
System.out.println(person);
person = new Instructor("Frank", "4315 Walnut Ct.", "4445555", "Professor");
System.out.println(person);
}
}
}
}
}
Click the link for the assignment description
I made some changes to your code the main mistake was while using inheritance try to use different files so that readability of code increases.
Whenever a child class extends a parent class make sure to completely close the parent class and then start the child else it leads to error.
Also there is no need to override toString() method each time instead you can just create a display method in the parent class and make the extra variables in the child classes private so that they can be accessed using getters and setters.
Main Class:
class Person {
public String name;
public String address;
public String phoneNumber;
public Person(String name, String address, String phoneNumber) {
this.name = name;
this.address = address;
this.phoneNumber = phoneNumber;
}
public void displayDetails() {
System.out.println("name is "+ this.name);
System.out.println("address is "+ this.address);
System.out.println("Phone number is "+this.phoneNumber);
}
}
class Student extends Person {
private String status;//made this private so that you can access using getters and setters
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Student(String name, String address, String phoneNumber, String status) {
super(name, address, phoneNumber);
this.status = status;
}
}
class Instructor extends Person {
private String rank;//made this private so that you can access using getters and setters
public Instructor(String name, String address, String phoneNumber, String rank) {
super(name,address,phoneNumber);
this.rank = rank;
}
public String getRank() {
return rank;
}
public void setRank(String rank) {
this.rank = rank;
}
}
Tester Class:(make sure to create this in a seperate file)
public class TestingCode {
public static void main(String [] args) {
Person person = new Person("a","b","c");
person.displayDetails();
System.out.println("-------------\n");
Student y = new Student("Susan", "123 2nd Ave.", "3334444", "Sophomore");
y.displayDetails();
System.out.println(y.getStatus());
System.out.println("-------------\n");
Person y1= new Student("Frank", "4315 Walnut Ct.", "4445555", "Sophomore");//dynamic binding from parent to child
y1.displayDetails();
System.out.println("-------------\n");
Instructor y2= new Instructor("Frank", "4315 Walnut Ct.", "4445555", "Professor");
y2.displayDetails();
System.out.println(y2.getRank());
}
}

The value of the field is not used

public class Client {
private String idNum;
private int driverLicence;
private String name;
private String surname;
private String mailAddress;
private String address;
private int phoneNum;
public Client(String idNum, int driverLicence, String name, String surname, String mailAddress, String address, int phoneNum) {
this.address=address;
this.driverLicence=driverLicence;
this.idNum=idNum;
this.mailAddress=mailAddress;
this.name=name;
this.phoneNum=phoneNum;
this.surname=surname;
}
public String getIdNum() {
return idNum;
}
public void setIdNum(String idNum) {
this.idNum = idNum;
}
public int getDriverLicence() {
return driverLicence;
}
public void setDriverLicence(int driverLicence) {
this.driverLicence = driverLicence;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public String getMailAddress() {
return mailAddress;
}
public void setMailAddress(String mailAddress) {
this.mailAddress = mailAddress;
}
public String getAddress() {
return Address;
}
public void setAddress(String address) {
this.address = address;
}
public int getPhoneNum() {
return phoneNum;
}
public void setPhoneNum(int phoneNum) {
this.phoneNum = phoneNum;
}
}
THE VALUE OF THE FIELD Client.idNum IS NOT USED
for some reason i am getting this kind of error on every field i have written on this class
ALL getters and setters are generated from eclipse
and all my other classes are fine but for some reason this specific class gives this "error"
i have wasted a lot of time on this and can't seem to find the reason why this happends
any ideas?
I copy pasted my code in, and an issue that may be causing your problem is that the code below returns the incorrect instance variable. Your instance variable is "address" not "Address".
public String getAddress() {
return Address;
}

Having problem getting getDiscount() from the parameter (JAVA)

I want to get discount from the getter in the employee class which extends person class. The discount varies upon hourlyWorked. I have set up parameters.
I can get the discount when i use setMethods. e.g if i use
e.setHourlyWorked(56);
System.out.println(e.getHourlyWorked());
System.out.println(e.getDiscount());
I get the result as expected.
But i cant get e.getDiscount()); from the parameter itself i.e
Employee e= new Employee(23, 22,"Nab","Kar","000");
System.out.println(e.getHourlyWorked());
System.out.println(e.getDiscount());
I get 0 as discount.
Person class
public class Person {
//private fields
private String firstName;
private String surname;
private String phoneNumber;
private int discount;
public Person(){
//Default Constructors
firstName="John";
surname="SMITH";
phoneNumber="0000";
}//constructors with all the inputs
public Person(String fn, String sn, String pn){
firstName=fn;
surname=sn;
phoneNumber=pn;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String fn) {
firstName=fn;
}
public String getSurname() {
return surname;
}
public void setSurname(String sn) {
surname = sn;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String pn) {
phoneNumber = pn;
}
#Override
public String toString(){
return getFirstName()+" "+getSurname()+" "+getPhoneNumber();
}
Employee class
package lollyShopSystem;
public class Employee extends Person {
private double hourlyWorked;
private double wage;
private int dis;
public Employee(){
//Default Constructors
hourlyWorked=30;
wage=34;
}
public Employee(double hourlyWorked, double wage, String fn, String sn, String pn) {
super(fn, sn, pn);
this.hourlyWorked = hourlyWorked;
this.wage = wage;
}
public double getHourlyWorked() {
return this.hourlyWorked;
}
public void setHourlyWorked(double hourlyWorked) {
this.hourlyWorked = hourlyWorked;
if (this.hourlyWorked<=20){
this.dis=5;
}
else if (this.hourlyWorked>20 && this.hourlyWorked<=30){
this.dis=10;
}
else {
this.dis=15;
}
}
public double getWage() {
return this.wage;
}
public void setWage(double wage) {
this.wage = wage;
}
public int getDiscount(){
return this.dis;
}
}
Assigning hourlyWorked using the constructor does not change the discount. You can replace the assignment of the field with the setHourlyWorked function instead.
public Employee(double hourlyWorked, double wage, String fn, String sn, String pn) {
super(fn, sn, pn);
setHourlyWorked(hourlyWorked);
this.wage = wage;
}

Do child/sub classes have to be in their own separate file in order to test? [duplicate]

This question already has answers here:
Why is each public class in a separate file?
(11 answers)
Closed 3 years ago.
I am using Eclipse to run this code program to test a Person class and its subclasses. In Eclipse it shows there are errors--that each child class must be defined in its own file.
I am learning Java, and would like to know if this is a must? Or can I make it work with parent and child classes all in one file? If I'm missing something, please point me in the right direction. Thank you!
Here is my code: [I put this is all in one file on Eclipse]
import java.util.*;
//Test program to test Person class and its subclasses
public class Test {
public static void main(String[] args) {
Person person = new Person("person");
Student student = new Student ("student");
Employee employee = new Employee("employee");
Faculty faculty = new Faculty("faculty");
Staff staff = new Staff("staff");
//invoke toString() methods
System.out.println(person.toString());
System.out.println(student.toString());
System.out.println(employee.toString());
System.out.println(faculty.toString());
System.out.println(staff.toString());
}
}
//Defining class Person
public class Person {
protected String name;
protected String address;
protected String phoneNum;
protected String email;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress () {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhoneNum() {
return phoneNum;
}
public void setPhoneNum(String phoneNum) {
this.phoneNum = phoneNum;
}
public String getEmail() {
return email;
}
public void setEmail (String email) {
this.email = email;
}
#Override
public String toString() {
return "Name:"+getName()+"Class:"+this.getClass().getName();
}
}
//Defines class Student extends Person
public class Student extends Person {
public static final String FRESHMAN = "freshman";
public static final String SOPHMORE = "sophmore";
public static final String JUNIOR = "junior";
public static final String SENIOR = "senior";
protected String classStatus;
public Student(String name) {
super(name);
}
public Student(String name, String classStatus) {
super(name);
this.classStatus = classStatus;
}
#Override
public String toString() {
return "Name:"+getName()+"Class:"+this.getClass().getName();
}
}
//Defines class Employee extends Person
public class Employee extends Person {
protected double salary;
protected String office;
protected MyDate dateHired;
public Employee(String name) {
this(name, 0, "none", new MyDate());
}
public Employee(String name, double salary, String office, MyDate dateHired) {
super(name);
this.salary = salary;
this.office = office;
this.dateHired - dateHired;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getOffice() {
return office;
}
public void setOffice (String office) {
this.office = office;
}
public MyDate getDateHired() {
return dateHired;
}
public void setDateHired(MyDate dateHired) {
this.dateHired = dateHired;
}
#Override
public String toString() {
return "Name:"+getName()+"Class:" + this.getClass().getName();
}
}
//Defines class Faculty extends Employee
public class Faculty extends Employee {
public static String LECTURER = "lecturer";
public static String ASSISTANT_PROFESSOR = "assistant professor";
public static String ASSOCIATE_PROFESSOR + "associate professor";
public static PROFESSOR = "professor";
protected String officeHours;
protected String rank;
public Faculty(String name) {
this(name, "9-5 PM", "Employee");
}
public Faculty(String name, String officeHours, String rank) {
super(name);
this.officeHours = officeHours;
this.rank = rank;
}
public String getOfficeHours() {
return officeHours;
}
public void setOfficeHours(String officeHours) {
this.officeHours = officeHours;
}
public String getRank() {
return rank;
}
public void setRank(String rank) {
this.rank=rank;
}
#Override
public String toString() {
return "Name:"+getName()+"Class:"+this.getClass().getName();
}
}
//Defines class Staff extends Employee
public class Staff extends Employee {
protected String title;
public Staff(String name) {
this(name, "none");
}
public Staff(String name, String title) {
super(name);
this.title=title;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
#Override
public String toString() {
return "Name:"+getName()+"Class:"+this.getClass().getName();
}
}
//Define class MyDate
public class MyDate {
private int month, day, year;
public MyDate (int month, int day, int year) {
this.day=day;
this.month=month;
this.year=year;
}
}
Yes, there should be one class per file. Moreover, you are using the MyDate class in the Employee class, which you need to extend and you cannot extends more than one class, so it's better use the predefined Date class which is present java.util.Date. Import this in the Employee class.
import java.util.Date;
instead of this:
public Employee(String name, double salary, String office, MyDate dateHired)
use:
public Employee(String name, double salary, String office, Date dateHired)
There are some careless mistakes:
in Employee class
public static String ASSOCIATE_PROFESSOR + "associate professor";
change to:
public static String ASSOCIATE_PROFESSOR = "associate professor";
Similarly in faculty class
public static String ASSOCIATE_PROFESSOR + "associate professor";
put = instead of +.
Now this code will work.
Yes it is a must. One class per file. Class can have inner classes. You can define subclasses as inner classes. But I recommend putting them in separate files and don't use inner classes.

Constructor for subclass says super() is undefined

I have made this class in java
package mainpackage;
public class Users {
public char username;
public char name;
public char surname;
public char department;
public int usersCounter;
public char getUsername() {
return this.username;
}
public void setUsername(char username) {
this.username = username;
}
public char getSurname() {
return this.surname;
}
public void setSurname(char surname) {
this.surname = surname;
}
public char getName() {
return this.name;
}
public void setName(char name) {
this.name = name;
}
public char getDepartment() {
return this.department;
}
public void setDepartment(char department) {
this.department = department;
}
public int getUsersCounter() {
return this.usersCounter;
}
public void setUsersCounter(int usersCounter) {
this.usersCounter = usersCounter;
}
public Users(char userName, char Name, char surName, char Department, int UsersCounter) {
username = userName;
name = Name;
surname= surName;
department = Department;
usersCounter = UsersCounter;
}
}
and i also made a subclass in which I try to create the constructor of the subclass,but it shows me an error in super(which i put inside the constructor of the subclass) saying that the constructor Users is undefined.I am using Eclipse and the only solution to this problem is to create a constructor Users,but it already exists!!!
package mainpackage;
public class Students extends Users {
static int registrationNumber;
#SuppressWarnings("static-access")
public int getNumber() {
return this.registrationNumber;
}
#SuppressWarnings("static-access")
public void setNumber(char registrationNumber) {
this.registrationNumber = registrationNumber;
}
public Students(char userName, char Name, char surName, char Department, int UsersCounter,int RegistrationNumber) {
super(userName, Name, surName, Department, UsersCounter);
registrationNumber = RegistrationNumber;
}
}

Categories