how to use toString method?

Ads

 
 
 

Share on Google+Share on Google+

zam
how to use toString method?
1 Answer(s)      7 years ago
Posted in : Java Beginners
i'm a new student, and learn with java.

How to create a class named Employee with name and salary. Make a class Manager inherit from Employee with an instance field named department. Supply a method toString() that prints the manager?s name, department and salary. Make another class named Director which inherits from Manager that has an instance field named carAllowanceAmount. Supply the toString() method for Director that prints all of its instance variables. Also, write a program name TestQ3 that instantiates an object of each of these classes and invokes the toString() method of each of these object.
(Note: all instance variables must be assigned with private accessibility and methods with public accessibility)

how to create the program?

help me please...
Ads
View Answers

January 30, 2010 at 2:44 PM


Hi Friend,

Try the following code:

/*class Person{
int year;
String name;
public Person(String name,int year){
this.year=year;
this.name=name;
}
public String toString() {
return "Name= " + name + ", Year= " +year;
}
}
class Student extends Person{
private String degree;
public Student(String name,int year,String degree){
super(name,year);
this.degree=degree;
}
public String toString() {
super.toString();
return "Name= "+super.name+", Year= "+super.year+", Degree= " +degree;
}

}
class Lecturer extends Person{
private int salary;
public Lecturer(String name,int year,int salary){
super(name,year);
this.salary=salary;
}
public String toString() {
return "Name= "+super.name+ ", Year= "+super.year+ ", Salary= " +salary;
}
}
public class TestQ2
{
public static void main(String[] args)
{
Person p = new Person("Angel", 1959);
Student s = new Student("Ane", 1979, "MCA");
Lecturer e = new Lecturer("Aurther", 1969, 65000);
System.out.println(p);
System.out.println(s);
System.out.println(e);

}
}*/
class Employee{
int salary;
String name;
public Employee(String n,int sal){
this.name=n;
this.salary=sal;
}
public String toString() {
return "Name= " + name + ", Salary= " +salary;
}
}
class Manager extends Employee{
private String department;
public Manager(String name,int salary,String dept){
super(name,salary);
this.department=dept;
}
public String toString() {
super.toString();
return "Name= "+super.name+", Salary= "+super.salary+", Degree= " +department;
}

}
class Director extends Employee{
private int carAllowanceAmount;
public Director(String name,int salary,int amount){
super(name,salary);
this.carAllowanceAmount=amount;
}
public String toString() {
return "Name= "+super.name+ ", Salary= "+super.salary+ ", Car Allowance Amount= " +carAllowanceAmount;
}
}
public class TestQ2
{
public static void main(String[] args)
{
Employee p = new Employee("Angel", 20000);
Manager s = new Manager("Ane", 30000, "Computer");
Director e = new Director("Aurther", 40000, 5000);
System.out.println(p);
System.out.println(s);
System.out.println(e);
}
}

Thanks

Ads









Tutorials   
Java Spring Hibernate Struts Training ClassNotFoundException HttpRequestInterceptor java.lang.noclassdeffounderror: org/apache/http/httprequest noclassdeffounderror: org/apache/http/client/methods/httpurirequest java.lang.NoClassDefFoundError: org/apache/http/client/HttpClient How do I resolve this Java Class not found exception? httpclient java.lang.NoClassDefFoundError Apache Commons ioutils maven dependency Read/Convert an inputStream to a String What is the meaning of Java Platform? Why Java is a platform independent language? What is the benefits of learning Core Java? Which technology should I learn after Java? What is array in java with example? How to Convert ArrayList to Array? How to substring in Java? How to format number in Java? What is instance variable in Java? How to download MySQL JDBC driver? What is Calendar class in Java? Which is the best Java tutorials for beginners? How to rename a file in Java? How to delete file in Java code? How to get day from date in Java using Calendar? How to get day of week in Java? How to calculate Date Difference in Java? How to compare date in Java? How to declare array in Java? How to calculate average of array in Java? What is Array in Java? write a java program to find the summation of all the integers entered on command line Sum of two numbers using command line arguments in Java How to create and use Array in Java? How to pass command line arguments in Java? How to create Applet Hello World? Appending String efficiently in Java How to append String in Java? How to list even numbers between 1 and 100? How to add BigDecimal in Java? What is Abstraction In Java? Which is best Beginners Java Tutorial? What is java.util package? Create list from array in Java Filter collection in Java 8 What is the best way to filter a Java Collection? Easy way to transform Collection to Array? How to convert Collection to Array in Java? What are Basic Java Language Elements? Advanced Java Tutorials in 2017 Java brief history Best Reasons to learn Java

Ads

 
Advertisement null

Ads