Okay, so I have 4 different classes for my program, and I'm trying to add a new variable in one of my subclasses. I will show the code for all of the classes involved and then explain what exactly is wrong.
First Code is my Person Class:
class Person {
private String myName; // name of the person
private int myAge; // person's age
private String myGender; // 'M' for male, 'F' for female
// constructor
public Person(String name, int age, String gender) {
myName = name;
myAge = age;
myGender = gender;
}
public String getName() {
return myName;
}
public int getAge() {
return myAge;
}
public String getGender() {
return myGender;
}
public void setName(String name) {
myName = name;
}
public void setAge(int age) {
myAge = age;
}
public void setGender(String gender) {
myGender = gender;
}
public String toString() {
return myName + ", age: " + myAge + ", gender: " + myGender;
}
}
my next class is my Teacher Class:
public class Teacher extends Person {
private static int salary;
private static String subject;
public Teacher(String name, int age, String gender, String subject, int salary) {
super(name, age, gender);
//Constructor
salary = salary;
subject = subject;
public String toString(){
return super.toString() +", Subject: " + subject + " Salary: " + salary;
}
}
}
So the problem with my program is, I'm trying to add in the String Subject and the Int Salary. I'm getting an error that keeps saying "Syntax error on token "String", # expected"(Line 12 of the Teacher Class) and then another one right next to it saying:""Syntax error, insert "EnumBody" to complete BlockStatement"(line 12 of the Teacher Class).
So all I am trying to do is add the new values into my parent class so that when I go to add in a user I can enter their salary and subject and so on. Sorry if i'm not too clear. It might be in plain sight for some of you, if i'm not clear enough please let me know.
Any help is greatly appreciated!
Thanks,
Sully
I haven't looked all through your code, but I believe this is the problem:
public Teacher(String name, int age, String gender, String subject, int salary) {
super(name, age, gender);
//Constructor
salary = salary;
subject = subject;
public String toString(){
return super.toString() +", Subject: " + subject + " Salary: " + salary;
}
You've started overriding toString in the middle of the constructor. You need to close the constructor, then start the toString method:
public Teacher(String name, int age, String gender, String subject, int salary) {
super(name, age, gender);
//Constructor
salary = salary;
subject = subject;
}
public String toString() {
return super.toString() + ", Subject: " + subject + " Salary: " + salary;
}
Ideally, use the #Override annotation to tell the compiler to validate that you really are overriding a superclass method, too:
#Override
public String toString() {
return super.toString() + ", Subject: " + subject + " Salary: " + salary;
}
One issue is:
public Teacher(String name, int age, String gender, String subject, int salary) {
super(name, age, gender);
//Constructor
salary = salary;
subject = subject;
public String toString(){
return super.toString() +", Subject: " + subject + " Salary: " + salary;
}
}
Your toString() method is inside constructor. Move it to outside the constructor.
public Teacher(String name, int age, String gender, String subject, int salary) {
super(name, age, gender);
//Constructor
salary = salary;
subject = subject;
}
public String toString(){
return super.toString() +", Subject: " + subject + " Salary: " + salary;
}
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
please teach me how to convert the code into a interface. What should be done is that there is a person class which has a name, address, gender, and phone. then a student, faculty, staff class which inherits the person class. but the student has a course and major, faculty has a rank and research area, and staff has a position and an office
this is my person code
//Person.java
class Person{
private String myName;
private String myAddress;
private char myGender;
private int myPhone;
public Person()
{
myName = "";
myAddress = "";
myGender = 'N';
myPhone = 0;
}
public Person(String name, String address, char gender, int phone){
myName = name;
myAddress = address;
myGender = gender;
myPhone = phone;
}
public String getName(){
return myName;
}
public String getAddress()
{
return myAddress;
}
public char getGender()
{
return myGender;
}
public int getPhone()
{
return myPhone;
}
public void setName(String name)
{
myName = name;
}
public void setAddress(String address)
{
myAddress = address;
}
public void setGender(char gender)
{
myGender = gender;
}
public void setPhone(int phone)
{
myPhone = phone;
}
public String toString()
{
return myName + ", address: " + myAddress + ", gender: " + myGender + ", phone: " + myPhone;
}
}
this is my student code
//Student.java
class Student extends Person
{
private String myCourse;
private String myMajor;
public Student()
{
super();
myCourse = "";
myMajor = "";
}
public Student(String name, String address, char gender, int phone,
String course, String major)
{
super(name, address, gender, phone);
myCourse = course;
myMajor = major;
}
public String getCourse()
{
return myCourse;
}
public String getMajor()
{
return myMajor;
}
public void setCourse(String course)
{
myCourse = course;
}
public void setMajor(String major)
{
myMajor = major;
}
public String toString()
{
return super.toString() + ", course: " + myCourse + ", major: " + myMajor;
}
}
this is my faculty code
//Faculty.java
public class Faculty extends Person
{
private String myRank;
private String myResearch;
public Faculty()
{
super();
myRank = "";
myResearch = "";
}
public Faculty(String name, String address, char gender, int phone, String rank, String research)
{
super(name, address, gender, phone);
myRank = rank;
myResearch = research;
}
public String getRank()
{
return myRank;
}
public String getResearch()
{
return myResearch;
}
public void setRank(String rank)
{
myRank = rank;
}
public void setResearch(String research)
{
myResearch = research;
}
public String toString() {
return super.toString() + ", rank: " + myRank + ", research: " + myResearch;
}
}
this is my staff code
//Staff.java
public class Staff extends Person
{
private String myPosition;
private String myOffice;
public Staff()
{
super();
myPosition = "";
myOffice = "";
}
public Staff(String name, String address, char gender, int phone, String position, String office)
{
super(name, address, gender, phone);
myPosition = position;
myOffice = office;
}
public String getPosition()
{
return myPosition;
}
public String getOffice()
{
return myOffice;
}
public void setPosition(String position)
{
myPosition = position;
}
public void setResearch(String office)
{
myOffice = office;
}
public String toString() {
return super.toString() + ", position: " + myPosition + ", office: " + myOffice;
}
}
enter image description here
please teach me how to convert the code into a interface
Basicly these are just the Models that your application is going to use. You cannot create a user interface having just these Models, but you can make it yourself.
In Java, you have various libraries which help you to create GUI, for example JavaFX or Swing (not really recommended today).
When it comes to the project architecture, what you can use is a design pattern called Model-View-Controller (MVC).
There are also other questions which consider basics of user interfaces in Java, which could help you:
How to create a GUI in Java
or
Create an application GUI by Javafx
And if you are still unsure of which GUI library you should use, see this question:
Java GUI frameworks. What to choose? Swing, SWT, AWT, SwingX, JGoodies, JavaFX, Apache Pivot?
I am fairly new to Inheritance, and I'm not sure if I am doing it right but I seem to be on the right track. The program runs fine except the output I am getting isn't right. I think the problem is to do with my constructors.
public class Person {
protected static String name;
protected static int birthYear;
public Person(String name, int birthYear) {
}
public String name (String n) {
n = name;
return n;
}
public int birthYear (int bY) {
bY = birthYear;
return bY;
}
#Override
public String toString() {
return String.format(name + birthYear);
}
}
public class Student extends Person {
protected String major;
public Student(String name, int birthYear, String major) {
super(name, birthYear);
major = "";
}
public String major(String maj) {
maj = major;
return maj;
}
public String toString() {
super.toString();
return super.toString() + major;
}
}
public class Instructor extends Person {
protected static int salary;
public Instructor(String name, int birthYear, int salary) {
super(name, birthYear);
salary = 0;
}
public int salary(int sal) {
sal = salary;
return sal;
}
public String toString() {
super.toString();
return super.toString() + salary;
}
}
public class PersonTester {
public static void main(String[] args) {
Person p = new Person("Perry", 1959);
Student s = new Student("Sylvia", 1979, "Computer Science");
Instructor e = new Instructor("Edgar", 1969, 65000);
System.out.println(p);
System.out.println("Expected: Person[name=Perry,birthYear=1959]");
System.out.println(s);
System.out.println("Expected:" +
"Student[super=Person[name=Sylvia,birthYear=1979],major=Computer]");
System.out.println(e);
System.out.println("Expected:" + "Instructor[super=Person[name=Edgar,birthYear=1969],salary=65000.0]");
}
}
OUTPUT I AM GETTING:
null0
Expected: Person[name=Perry,birthYear=1959]
null0null
Expected: Student[super=Person[name=Sylvia,birthYear=1979],major=Computer Science]
null00
Expected: Instructor[super=Person[name=Edgar,birthYear=1969],salary=65000.0]
Try changing your constructor in Person to:
public Person(String name, int birthYear) {
this.name = name;
this.birthYear = birthYear;
}
Currently, the constructor has an empty body, so when you call super(name, birthYear); in the subclass constructor, nothing actually happens.
Your Student constructor also has an error. You forgot to initialize the major field.
public Student(String name, int birthYear, String major) {
super(name, birthYear);
this.major = major;
}
You have the same problem in the Instructor constructor...
public Instructor(String name, int birthYear, int salary) {
super(name, birthYear);
this.salary = salary;
}
Finally, you need to take away the static keywords before the fields in Person. This is because static ensures, that there will always be one (and only one) instance of those fields per class, as opposed to one per instance, like you want it to be:
protected String name;
protected int birthYear;
Same thing for the salary field in Instructor.
n = name; this causing your problem. It must be name = n;. All your setter function contain this problem, correct them all and tell me result.
I have to design a class that consists of 2 subs-classes. Let's say A and B and B has 2 subs-classes too. This part is really not important because I did all this with different names.
I am struggling with trying to run my code. I have the code but I am getting 2 errors. I can't figure out how to fix it.
The errors are the following:
aaa1.java:147: cannot find symbol
symbol : constructor Employee(java.lang.String,java.lang.String,java.lang.String,java.lang.String)
location: class Employee
super(name, address, phone, email);
^
aaa1.java:179: cannot find symbol
symbol : constructor Employee(java.lang.String,java.lang.String,java.lang.String,java.lang.String)
location: class Employee
super(name, address, phone, email);
My code:
import java.util.*;
public class aaa1 {
public static void main(String[] args){
Person p = new Person("J11h", "123 ABC St", "908-123-456", "asdf123#gmail.com");
Student s = new Student("222se1h", "123 ABC St", "908-123-456", "asdf123#gmail.com", 1);
Date date = new Date();
Employee e = new Employee("wewesh", "123 ABC St", "908-123-456", "asdf123#gmail.com", "123", 2000.00, date);
Faculty f = new Faculty("eewrr", "123 ABC St", "908-123-456", "asdf123#gmail.com", "1pm - 3pm", "Head of the department");
Staff st = new Staff("rrereh", "123 ABC St", "908-123-456", "asdf123#gmail.com", "Assistant");
System.out.println(p.toString());
System.out.println(s.toString());
System.out.println(e.toString());
System.out.println(f.toString());
System.out.println(st.toString());
}
}
class Person {
//A person has a name, address, phone number, and email address.
String name;
String address;
String phone;
String email;
public Person(String name, String address, String phone, String email) {
this.name = name;
this.address = address;
this.phone = phone;
this.email = email;
}
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 getPhone(){
return phone;
}
public void setPhone(String phone){
this.phone = phone;
}
public String getEmail(){
return email;
}
public void setEmail(String email){
this.email = email;
}
//Overriding
public String toString(){
return "The person's information is "+name+ ", " +address+ ", " +phone+ ", " +email;
}
}
class Student extends Person{
//A student has a class status
//(freshman,sophomore, junior, or senior).
//Define the status as a constant.
final int freshman =1;
final int sophomore =2;
final int junior=3;
final int senior=4;
int status;
public Student(String name, String address, String phone, String email, int status) {
super(name, address, phone, email);
this.status = status;
}
public int getStatus(){
return status;
}
public void setStatus(int status){
this.status = status;
}
//Overriding
public String toString(){
return "The student's information is: " +super.toString()+ "," + status;
}
}
class Employee extends Person{
//An employee has an office, salary, and date hired.
String office;
double salary;
java.util.Date dateHired;
public Employee(String name, String address, String phone, String email, String office, double salary, Date dateHired){
super(name, address, phone, email);
this.office = office;
this.salary = salary;
this.dateHired = dateHired;
}
public String getOffice(){
return office;
}
public void setOffice(String office){
this.office = office;
}
public double getSalary(){
return salary;
}
public void setSalary(double salary){
this.salary = salary;
}
public void setDateHired(Date dateHired){
this.dateHired = dateHired;
}
//Overriding
public String toString(){
return "The Employee's information is: " +super.toString()+ ", " +office+ ", " +salary+ ", " +dateHired;
}
}
class Faculty extends Employee{
//A faculty member has office hours and a rank.
String officeHours;
String rank;
public Faculty(String name, String address, String phone, String email, String officeHours, String rank){
super(name, address, phone, email);
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;
}
//Overriding
public String toString(){
return "The faculty's information is: " +super.toString()+ ", " + officeHours + ", " + rank;
}
}
class Staff extends Employee{
//A staff member has a title
String title;
public Staff(String name, String address, String phone, String email, String title){
super(name, address, phone, email);
this.title = title;
}
public String getTitle(){
return title;
}
public void setTitle(String title){
this.title = title;
}
//Overriding
public String toString(){
return "The staff's information is: " +super.toString()+ "," + title;
}
}
Can you please help me?
Your Employee class contains a constructor with the following parameters:
public Employee(String name, String address, String phone, String email, String office, double salary, Date dateHired){
And your Faculty and Staff classes, which extend Employee, contain this explicit call to super:
super(name, address, phone, email);
The number of arguments in the call to super and the number of arguments in the Employee class constructor do not match; you're providing four String arguments when it needs one additional String, a double, and a java.util.Date.
Faculty - the class at line 147 - extends Employee, so you need to invoke a super constructor from Employee:
public Employee(String name, String address, String phone, String email, String office, double salary, Date dateHired){
i.e. you need to pass all of these parameters if you want to instantiate a subclass of Employee such as Faculty.
Alternatively, you could declare another constructor in Employee which takes just those 4 parameters. However, you would then need to decide what to do about the parameters that you have not passed in - do you simply set them to null, or do you set them to a safe default? This depends upon your application.
I believe that as you have it extending you need to also include the original constructor, so you have to have the same constructor as a person in the student staff and employee (with an #Override )
even if it just calls super();
so that is a Constructor (string,string,string,string);
Employee(java.lang.String,java.lang.String,java.lang.String,java.lang.String)
You will find the problem if you think a while of:
What will be the office, salary and hired date for a Faculty or a Staff member?
I have created a (Person,Student,Employee,Faculty and Staff)classes. Person has to subclasses Student and Employee.
Employee has two subclasses Faculty and Staff. I have done all the codings an they are working fine except my driver class TestPerson program its giving compilation errors
Note: A test program that Creates a Person,Student,Employee,Faculty,Staff, and invokes their toString Method.
The errors of driver class TestPerson.java are below:-
error: constructor Student in class Student cannot be applied to given types;
error: no suitable constructor found for Employee(String,String,String,String)
error: constructor Faculty in class Faculty cannot be applied to given types;
error: no suitable constructor found for Staff(String,String,String,String)"
**I am just providing the codes of the driver class. If you need my other codings of the other classes, please state in the comment, and I will immediately post it.
Please see my codings below:-
public class TestPerson {
public static void main(String[] args) {
Person person = new Person("John Doe", "123 Somewhere", "415-555-1212", "johndoe#somewhere.com");
Person student = new Student("Mary Jane", "555 School Street", "650-555-1212", "mj#abc.com", "junior");
Person employee = new Employee("Tom Jones", "777 B Street", "40-88-889-999", "tj#xyz.com");
Person faculty = new Faculty("Jill Johnson", "999 Park Ave", "92-52-22-3-333", "jj#abcxyz.com");
Person staff = new Staff("Jack Box", "21 Jump Street", "707-21-2112", "jib#jack.com");
System.out.println(person.toString() + "\n");
System.out.println(student.toString() + "\n");
System.out.println(employee.toString() + "\n");
System.out.println(faculty.toString() + "\n");
System.out.println(staff.toString() + "\n");
}
}
//Person Class
public class Person {
private String name;
private String address;
private String phone_number;
private String email_address;
public Person() {
}
public Person(String newName, String newAddress, String newPhone_number, String newEmail){
name = newName;
address = newAddress;
phone_number = newPhone_number;
email_address = newEmail;
}
public void setName(String newName){
name = newName;
}
public String getName(){
return name;
}
public void setAddress(String newAddress){
address = newAddress;
}
public String getAddress(){
return address;
}
public void setPhone(String newPhone_number){
phone_number = newPhone_number;
}
public String getPhone(){
return phone_number;
}
public void setEmail(String newEmail){
email_address = newEmail;
}
public String getEmail(){
return email_address;
}
public String toString(){
return "Name :"+getName();
}
}
//Student class
public class Student extends Person {
public final String class_status;
public Student(String name, String address, int phone, String email, String classStatus) {
super(name, address, phone, email);
class_status = classStatus;
}
public String toString(){
return "Student Status: " + super.getName();
}
}
//Employee Class
import java.util.Date;
public class Employee extends Person{
private String office;
private double salary;
private Date hire;
public Employee() {
}
public Employee(String name, String address, int phone, String email){
super(name, address, phone, email);
}
public Employee(String office, double salary, Date hire){
this.office = office;
this.salary = salary;
this.hire = hire;
}
public void setOffice(String office){
this.office = office;
}
public String getOffice(){
return this.office;
}
public void setSalary(double salary){
this.salary = salary;
}
public double getSalary(){
return this.salary;
}
public void setHire(Date hire){
this.hire = hire;
}
public Date getHire(){
return this.hire;
}
public String toString(){
return "Office " + super.getName();
}
}
//Faculty Class
public class Faculty extends Employee {
private String officeHours;
private int rank;
public Faculty(String name, String address, int phone, String email) {
super(name, address, phone, email);
}
public String toString(){
return "Office " + super.getOffice();
}
}
//Staff Class
public class Staff extends Employee {
private String title;
public Staff(String name, String address, int phone, String email) {
super(name, address, phone, email);
}
public Staff(String title){
this.title = title;
}
public void setTitle(String title){
this.title = title;
}
public String getTitle(){
return this.title;
}
public String toString(){
return "Title :" + super.getName();
}
}
The reason you are getting those errors is that the constructors don't exist.
error: constructor Student in class Student cannot be applied to given
types; error: no suitable constructor found for
Employee(String,String,String,String)
That means you will not get the code to compile until you have this:
Student(String name, String addr, String phone, String email) {
....
}
Assuming you have set the properties in the constructor, toString would look like this:
public String toString() {
return this.name + "\n" + this.addr + "\n" + this.phone + "\n" + this.email;
}
UPDATE
Your problem is that Student has only this constructor:
public Student(String name, String address, int phone, String email, String classStatus)
Student needs a constructor which takes only four strings as its parameters. Alternatively, you can make everything take the five parameters you specified.
It's perhaps not related to the question itself, but I think the design could be refined like this:
define abstract class Role with the name of role
define classes Student, Employee, Staff whatever inheriting Role
define class Person with common properties for all person type, names etc, and having property of type Role inside
then define toString on Person and in all Role implementation
in this way you will be able to extend or modify Persons independently from Roles, which makes the design more flexible
The constructor of Person requires a String as third argument, but you are trying to pass int phone to the super-constructor in your sub-classes. That won't work because it's the wrong type.
By the way: you should always represent phone numbers with strings, not with integers.
It doesn't make sense to add or subtract phone numbers.
Integers don't allow leading zeros, which are used for area codes in some
countries.
Integers can't be larger than 2147483648. When you
try to store very long telephone numbers, this seemingly arbitrary
limit will cause very unexpected bugs.
First Set the Phone Number datatype as integer in all the classes ..
main Function will be:
public class TestPerson {
public static void main(String[] args) {
Person person = new Person("John Doe", "123 Somewhere", "415-555-1212",
"johndoe#somewhere.com");
Person student = new Student("Mary Jane", "555 School Street",
650-555-1212, "mj#abc.com", "junior");
Person employee = new Employee("Tom Jones", "777 B Street",
40-88-889-999, "tj#xyz.com");
Person faculty = new Faculty("Jill Johnson", "999 Park Ave",
92-52-22-3-333, "jj#abcxyz.com");
Person staff = new Staff("Jack Box", "21 Jump Street", 707-21-2112,
"jib#jack.com");
System.out.println(person.toString() + "\n");
System.out.println(student.toString() + "\n");
System.out.println(employee.toString() + "\n");
System.out.println(faculty.toString() + "\n");
System.out.println(staff.toString() + "\n");
}
}
Ye've been very helpful so far even though I havn't been great at wording my questions. I think I almost know what I'm doing but I'm trying to get my head around the relationship between getters, setters and constructors. I have two classes as follows
Student and Name and I'm confused about the relationship between the parameters in the getters and setters and constructor
If I remove the parameters from the Name constructor, i.e. this
public Name (){
this.firstName = firstName;
this.lastName = lastName;
and aslo edited the Student constructor to reflect this
public Student(int id, String firstName, String lastName, String street, String area, String city, String country, int age, char gender, String college, String course, int level, int gradePointAverage){ //This is the list of variables called from the Student class
this.id = id;
this.name = new Name(); //I have removed the parameters here
this.name.setFirstName(firstName);
this.name.setLastName(lastName);
this.address = new Address(street, area, city, country);
this.age = age;
this.gender = gender;
this.college = college;
this.course = course;
this.level = level;
this.gradePointAverage = gradePointAverage;
}
I'm not sure what impact it would have. I would be very greatful of someone could explain to me where I should/shouldn't have parameters and why? Understand this concept is what is holding me back from moving any further with coding at the moment.
public class Student {
private Name name; // This is calling from the Name class, giving it the name 'name'
private Address address; // This calls from Address, giving it the name 'address'
private char gender;
private String course, college;
private int gradePointAverage, id, age, level;
public Student(int id, String firstName, String lastName, String street, String area, String city, String country, int age, char gender, String college, String course, int level, int gradePointAverage){ //This is the list of variables called from the Student class
this.id = id;
this.name = new Name(firstName, lastName);
//this.name = new Name();
this.name.setFirstName(firstName);
this.name.setLastName(lastName);
this.address = new Address(street, area, city, country);
this.age = age;
this.gender = gender;
this.college = college;
this.course = course;
this.level = level;
this.gradePointAverage = gradePointAverage;
}
public int getId(){
return id;
}
public String getName(){
return name.toString();
}
public String getAddress(){
return address.toString();
}
public int getAge(){
return age;
}
public char getGender(){
return gender;
}
public String getCollege(){
return college;
}
public int getLevel() {
return level;
}
public String getCourse() {
return course;
}
public int getGradePointAverage() {
return gradePointAverage;
}
public void printStudent() {
System.out.println("The Student " + name.toString() + " is logged under the student ID number " + id + ".");
System.out.println("They live at " + address.toString() + " and their age is " + age + ".");
System.out.println("Their gender is " + gender + ".");
System.out.println("The student studies at " + college + " attending classes in " + course + ".");
System.out.println("Their level is " + level + " and the student grade average in points is " + gradePointAverage + ".");
System.out.println();
}
}
and Name
public class Name{
private String firstName, lastName;
public Name (String firstName, String lastName){
//public Name (){
this.firstName = firstName;
this.lastName = lastName;
}
public void setFirstName(String firstName){
this.firstName = firstName;
}
public void setLastName(String lastName){
this.lastName = lastName;
}
//public String getFirstName() {
// return firstName;
//}
//public String getLastName() {
// return lastName;
//}
///** Returns first name concatenated to last name */
//public String toString() {
// return firstName + " " + lastName;
//}
}
The constructors are for initialization of an instance of an Object to ensure that all the minimum amount of data required for a valid object state are provided at creation time.
In your case Name should have a constructor that requires firstName and lastName because those things are what make the Name object fully initialized. This Object should work just like your Address object you have shown as well.
Otherwise if you use setXXX methods, the Name object is incomplete with the two String objects initialized to null or some other undefined state.