Shayan's Software & Technology

My adventure as a Software Engineer continues..,

Lab1: JAVA Intro

package lab1;
import java.util.*;
/**
*
* @author: Shayan Zafar Ahmad
*  Lab1
*/
public class student {
private int id;
private String name, program;
public student(){}
public student(String name, String program, int id){
if (name == null || program == null)
throw new NullPointerException();
else{
this.name = name;
this.program = program;
}
this.id = id;
}//student(String,String,int)
public String getProgram() {return program;}
public String getName() {return name;}
public int getID() {return id;}
public void setProgram(String s){program = s;}
public void setName(String s){name = s;}
public void setId(int i){id = i;}
public String toString(){
return “Student Id:” + id + “\nStudent Name:” + name +
“\nProgram: ” + program;
}
public boolean equals(Object source){
if (source == null || !(source instanceof student))
return false;
student student1 = (student)source;
return student1.equals(name) && student1.equals(program) && student1.equals(id);
}
public int hashcode(){return 31*program.hashCode() + name.hashCode();}
public Object clone(){
student theClone = new student();
theClone.program = new String(program);
theClone.name = new String(name);
theClone.id = id;
return theClone;
}
public static void main(String[] args){
student student1 = new student(“Shayan Ahmad”,”Bachelors of Software Development”, 1001);
System.out.println(“Student 1: “+student1);
student student2 = new student();
student2.setName(“Rick Eyre”);
student2.setProgram(“Phd Physics”);
student2.setId(1002);
System.out.println(“Student 2:” + student2);
boolean equal = student1.equals(student2);
System.out.println(“Are they the same student? ” +equal);
System.out.println(“Displaying Formatted:”);
System.out.println(student1.toString());
System.out.println(student2.toString());
}
}

Output  using the NetBeans IDE:

run:

Student 1: Student Id:1001

Student Name:Shayan Ahmad

Program: Bachelors of Software Development

Student 2:Student Id:1002

Student Name:Rick Eyre

Program: Phd Physics

Are they the same student? false

Displaying Formatted:

Student Id:1001

Student Name:Shayan Ahmad

Program: Bachelors of Software Development

Student Id:1002

Student Name:Rick Eyre

Program: Phd Physics

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: