JAVA OBJECT ORIENTED PROGRAMMING – 10
MARK ANSWERS (WITH CODE)
1. Student Details using Reference Variables
class Student {
int rollNo;
String name, branch, mobile, address;
}
public class Main {
public static void main(String[] args) {
Student S1 = new Student();
Student S2 = new Student();
[Link] = 101;
[Link] = "Rahul";
[Link] = "CSE";
[Link] = "9876543210";
[Link] = "Bangalore";
[Link] = 102;
[Link] = "Anita";
[Link] = "ECE";
[Link] = "9123456789";
[Link] = "Mysore";
[Link]([Link] + " " + [Link]);
[Link]([Link] + " " + [Link]);
}
}
2. OOP Principles
class Person {
private String name;
public void setName(String n){ name = n; }
public String getName(){ return name; }
public void role(){ [Link]("Person"); }
}
class Student extends Person {
public void role(){ [Link]("Student"); }
}
public class Main {
public static void main(String[] args) {
Person p = new Student();
[Link]("Amit");
[Link]();
}
}
8. Factorial using Command Line Arguments
public class Main {
public static void main(String[] args) {
int n = [Link](args[0]);
int fact = 1;
for(int i=1;i<=n;i++)
fact *= i;
[Link]("Factorial = " + fact);
}
}