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());
}
}
Related
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;
}
I have a class AddressBook with the attributes long phoneNumber, Address tempAddress, Address permAddress where Address is the inner class of AddressBook. Its attributes are Name, City, Street, State. Both classes have getters and setters for all the attributes.
There is another class TestApplication where I need to test the working of the application by creating AddressBook object and print permanent, temporary address and phone number in the main method.
I could not figure out the way as to how to get to those Address class attributes into TestApplication, because of the confusing getters and setters of permAddress and tempAddress attributes.
error description
What should be the right approach? Not able to implement the getters and setters of attributes tempAddress and permAddress of type Address.
So far this is the code of AddressBook.java and TestApplication.java.
public class AddressBook{
private long phoneNumber;
private Address tempAddress;
private Address permAddress;
public void setPhoneNumber(long phoneNumber)
{
this.phoneNumber=phoneNumber;
}
public void setTempAddress(Address tempAddress)
{
this.tempAddress=tempAddress;
}
public void setPermAddress(Address permAddress)
{
this.permAddress=permAddress;
}
public long getPhoneNumber()
{
return phoneNumber;
}
public Address getTempAddress()
{
return tempAddress;
}
public Address getPermAddress()
{
return permAddress;
}
class Address{
private String name;
private String street;
private String city;
private String state;
public void setName(String name)
{
this.name=name;
}
public void setCity(String city)
{
this.city=city;
}
public void setStreet(String street)
{
this.street=street;
}
public void setState(String state)
{
this.state=state;
}
public String getName()
{
return name;
}
public String getStreet()
{
return street;
}
public String getCity()
{
return city;
}
public String getState()
{
return state;
}
}
}
import java.util.*;
public class TestApplication {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the permanent address");
System.out.println("Enter the house name");
String hn=sc.nextLine();
System.out.println("Enter the street");
String stee=sc.nextLine();
System.out.println("Enter the city");
String city=sc.nextLine();
System.out.println("Enter the state");
String state=sc.nextLine();
AddressBook a=new AddressBook();
AddressBook.Address b=a.new Address();
b.setName(hn);
b.setStreet(stee);
b.setCity(city);
b.setState(state);
System.out.println("Permanent address");
AddressBook ad=new AddressBook();
System.out.println("House name:"+ad.getPermAddress().getName());
System.out.println("Street:"+ad.getPermAddress().getStreet());
System.out.println("City:"+ad.getPermAddress().getCity());
System.out.println("State:"+ad.getPermAddress().getState());
System.out.println("Enter the temporary address");
System.out.println("Enter the house name");
String house=sc.nextLine();
System.out.println("Enter the street");
String street1=sc.nextLine();
System.out.println("Enter the city");
String city1=sc.nextLine();
System.out.println("Enter the state");
String state1=sc.nextLine();
System.out.println("Enter the phone number");
long ph=sc.nextLong();
System.out.println("Temporary address");
System.out.println("House name:"+ad.getTempAddress().getName());
System.out.println("Street:"+ad.getTempAddress().getStreet());
System.out.println("City"+ad.getTempAddress().getCity());
System.out.println("State:"+ad.getTempAddress().getState());
ad.setPhoneNumber(ph);
System.out.println("Phone number"+ad.getPhoneNumber);
}
}
So getters and setters are non-static methods (in most cases). What this means is that you will have to instantiate the object and then call the methods. So what you have to do is create Address instance Address a = new Address (...); and then you can use the instance to call your getters and setters, i.e. a.getZipCode(); etc
Use sc.next() instead of sc.nextLine() while taking the user input.
Because the inner class isn't public you need to get the Adress attributes like:
gettempAdresse().getName();
First you return an object of the type Adresse with "gettempAdresse()" and then you call the "getName()" method of this object which returns the name.
Edit:
You need to add constructors to each class first.
public AddressBook() {
tempAddress = new Address();
permAddress = new Address();
}
public Address(String name, String street, String city, String state) {
this.name = name;
this.street = street;
this.city = city;
this.state = state;
}
public Address() {
this("", "", "", "");
}
After this, you can create a instence of AddressBook and use the getters and setters like this:
AddressBook addressBook = new AddressBook(); //create instence of AddressBook
addressBook.setPermAddress(addressBook.new Address()); //set permAddress to a new Address
addressBook.getPermAddress().setName("name of permAddress"); //set name of permAddress
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.
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;
}
}
I have a doubt!
I would like to use a class with JavaBean to DTO converter methods and vice versa.
Is my web application, should the methods be synced or should I create an instance of this class when I want to use them?
This is the javabean:
public class Person {
private String name;
private String surname;
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;
}
}
This is the dto:
public class PersonDTO {
private String fullName;
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
}
This is the converter class:
public class PersonConverter {
public static PersonDTO fromBeanToDTO (Person Person) {
PersonDTO personDTO = new PersonaDTO();
personDTO.setFullName(person.getNome() + " " + person.getSurname());
return personDTO;
}
}