program to create real world object
0program to create real world object
/* Write a program to create real world object called student in Java. Create to real world student object from the class with data.
rollno =1
name =james
institute =netlojava
rollno =2
name =gosling
institute =netlojava */
//Student.java
class Student {
int rollNo;
String name;
String institute;
Student(int rn,String nm,String ins){
rollNo=rn;
name=nm;
institute=ins;
}
}
//NetloJavaInstitute.java
class NetloJavaInstitute{
public static void main(String args[]){
Student s1=new Student(1,"james","netlojava");
Student s2=new Student(2,"gosling","netlojava");
System.out.println("Student 1 Object Data");
System.out.println("Roll No:"+s1.rollNo);
System.out.println("Name:"+s1.name);
System.out.println("Institute:"+s1.institute);
System.out.println("\nStudent 2 Object Data");
System.out.println("Roll NO:"+s2.rollNo);
System.out.println("Name:"+s2.name);
System.out.println("Institute:"+s2.institute);
}
}
compilation:
G:\netlojava>javac NetloJavaInstitute.java
Execution:
G:\netlojava>java NetloJavaInstitute
Out Put:
Student 1 Object Data
Roll No:1
Name:james
Institute:netlojava
Student 2 Object Data
Roll NO:2
Name:gosling
Institute:netlojava
Note: In student class constraintes replace parameter names with non-static variable names as shown below.
Student(int rollNo,String name,String institute) { rollNo=rollNum; name=name; institute=institute; } then compile and execute NetloJavaInstitute.java
OutPut:
Student 1 Object Data Roll No:0 Name:null Institute:null Student 2 Object Data Roll NO:0 Name:null Institute:null
//Real World Object creation.Reduce and redundancy.
class Employee
{
int empid;
String name;
String job;
Employee(int eid,String en,String ej)
{
empid=eid;
name=en;
job=ej;
}
void Display()
{
System.out.println("\n Employee Id "+empid+" Details");
System.out.println("Employee Id:"+empid);
System.out.println("Employee Name:"+name);
System.out.println("Employee JOb:"+job);
}
}
class CEmployee
{
public static void main(String[] args)
{
Employee e1=new Employee(1,"Smith","Java Developer");
Employee e2=new Employee(2,"John","Java Developer");
Employee e3=new Employee(3,"Scott","java developer");
e1.Display();
e2.Display();
e3.Display();
}
}
OutPut:
Employee Id 1 Details
Employee Id:1
Employee Name:Smith
Employee JOb:Java Developer
Employee Id 2 Details
Employee Id:2
Employee Name:John
Employee JOb:Java Developer
Employee Id 3 Details
Employee Id:3
Employee Name:Scott
Employee JOb:java developer
0 comments: