Professional Documents
Culture Documents
Am 1
Am 1
BS(SE) 2019
NAME:ARMEEN JAVED
ENROLLMENT.NO:24308
DATE: 18-NOV-19
If a == 5
Print(a);
Question 3: What would the equivalent code, using a while loop, be for the example.
int i;
for(i = 0; i < 3; i = i + 1)
{
System.out.println ("a\n");
System.out.println ("b\n");
}
System.out.println ("c\n");
Output:
Question 5: What is the purpose of the curly braces in the ‘if statement’?
Answer:
Braces are used to group statement and declaration.The content of a class or interface are enclosed in
braces.Methood bodies and constructor bodies are enclosed in braces. Braces are used to group the
statement in an if statement, a loop, or other control structure.
Question 6: What is the difference between ++var and var++?
Answer:
The operator is placed bofore the variable (++var) the variable is incremented by 1 before it is
used in the expression. When the operator is placed after the variable (var++) the expression is
evaluated and then the variable is incremented by 1.
Question 7: What is the difference between declaring a variable and defining a variable?
Answer:
Declaration Variable Defining Variable
1. A variable or a function can be declared any 1. A variable or a function can be defined only
number of times . once .
ASSIGNMENT 1 Object Oriented Programming
BS(SE) 2019
NAME:ARMEEN JAVED
ENROLLMENT.NO:24308
DATE: 18-NOV-19
class ReverseString
{
public static void main(String args[])
{
String original, reverse = "";
Scanner in = new Scanner(System.in);
Question 9: Make a program which displays a different message depending on the age given. Here are
the possible responses:
ASSIGNMENT 1 Object Oriented Programming
BS(SE) 2019
NAME:ARMEEN JAVED
ENROLLMENT.NO:24308
DATE: 18-NOV-19
Note: A person who is under 16 will display three messages, one for being under 16, one for also being
under 18, and one for also being under 25.
Source code:
import java.util.*;
public class Test
{
public static void main(String args[])
{
System.out.println("Please enter a number");
Scanner a = new Scanner(System.in);
int age=a.nextInt();
if(age<=16)
{
System.out.println("You can't drive");
System.out.println("You can't vote");
System.out.println("You can't do anything that's legal");
}
else if(age<18)
{
System.out.println("You can't vote");
}
else if(age<25)
{
System.out.println("You can't drive");
}
else if(age<=25)
{
System.out.println("You can't do anything that's legal");
}
}
ASSIGNMENT 1 Object Oriented Programming
BS(SE) 2019
NAME:ARMEEN JAVED
ENROLLMENT.NO:24308
DATE: 18-NOV-19
}
Output:
Question 10: Create an array that can hold ten integers. Fill up each slot of the array with a random
number from 1 to 100. Then display the contents of the array on the screen.
Sample output:
Slot 0 contains a 45
Slot 1 contains a 87
Slot 2 contains a 39 `
Slot 3 contains a 32
Slot 4 contains a 93
Slot 5 contains a 86s
Slot 6 contains a 16…….
Source code:
public class Test
{
public static void main(String args[])
{
int [] a = new int[10];
a[0]=15;
a[1]=21;
a[2]=45;
a[3]=54;
a[4]=71;
a[5]=67;
a[6]=89;
a[7]=79;
a[8]=55;
a[9]=54;
for( int i=0; i<a.length;i++)
ASSIGNMENT 1 Object Oriented Programming
BS(SE) 2019
NAME:ARMEEN JAVED
ENROLLMENT.NO:24308
DATE: 18-NOV-19
{
System.out.println("The random numbers are:"+a[i]);
}
}
}
Output:
Question 11: Create a new project, and include in it the class Person that you just created. Create a
class "Student" and another class "Teacher", both descendants of "Person". The class "Student" will
have a public method "GoToClasses", which will write on screen "I’m going to class." The class
"Teacher" will have a public method "Explain", which will show on screen "Explanation begins". Also, it
will have a private attribute "course", a string. The class Person must have a method "SetAge (int n)"
which will indicate the value of their age (e.g., 20 years old). The student will have a public method
"ShowAge" which will write on the screen "My age is: 20 years old" (or the corresponding number).
You must create another test class called "StudentAndTeacherTest" that will contain "Main" and:
Source code:
Person
class Person
{
int age;
ASSIGNMENT 1 Object Oriented Programming
BS(SE) 2019
NAME:ARMEEN JAVED
ENROLLMENT.NO:24308
DATE: 18-NOV-19
int getAge()
{
return age;
}
void setAge(int age)
{
age=age;
}
void hello()
{
System.out.println("Hello");
}
}
Student
class Student extends Person
{
void GoToClasses()
{
System.out.println("I am going to class");
}
void ShowAge()
{
System.out.println("My age is 20 years old");
}
}
Teacher
class Teacher extends Person
{
private String course;
void Explain()
{
System.out.println("Explanation begins");
}
String getcourse()
{
ASSIGNMENT 1 Object Oriented Programming
BS(SE) 2019
NAME:ARMEEN JAVED
ENROLLMENT.NO:24308
DATE: 18-NOV-19
return course;
}
void setcourse (String course)
{
course=course;
}
void ShowAge()
{
System.out.println("My age is 30 years old");
}
}
Test
class StudentAndTeacherTest
{
public static void main(String args[])
{
Person p = new Person();