Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

ASSIGNMENT 1 Object Oriented Programming

BS(SE) 2019
NAME:ARMEEN JAVED
ENROLLMENT.NO:24308
DATE: 18-NOV-19

Question 1: What is the difference between = (single-equal) and == (double-equal)?


Answer:
Single equal"=" when you're assigning a value to a variable.
a=9

Double equal"==" is used to check some conditions.

If a == 5

Print(a);

Question 2: How can you round a floating point number?


Answer:
In order to round float numbers in Java, we use the java.lang.Math.round() method. The
method accepts float values and returns an integer value. This is computed by adding ½
to the number and then flooring it.

Question 3: What would the equivalent code, using a while loop, be for the example.

for (i = 0; i < 10; i = i + 1)


System.out.println ("i is” + i);
Answer:
{
int i = 0;
while (I < 10) {
i +=1;
}
System.out.println(“I is” +i);
}
ASSIGNMENT 1 Object Oriented Programming
BS(SE) 2019
NAME:ARMEEN JAVED
ENROLLMENT.NO:24308
DATE: 18-NOV-19

Question 4: What would this code print?

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

2. Memory will not be allocated during 2. Memory will be allocated.


declaration.
3. Declaration variable 3 .Defining variable
int x(int) ; int x (int a)
{
return a;
}

Question 8: Write a program to reverse a string.


Code:
import java.util.*;

class ReverseString
{
public static void main(String args[])
{
String original, reverse = "";
Scanner in = new Scanner(System.in);

System.out.println("Enter a string to reverse");


original = in.nextLine();

int length = original.length();

for (int i = length - 1 ; i >= 0 ; i--)


reverse = reverse + original.charAt(i);

System.out.println("Reverse of the string: " + reverse);


}
}
Output:

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

 age is less than 16, say "You can't drive."


 age is less than 18, say "You can't vote."
 age is less than 25, say "You can't rent a car."
 age is 25 or over, say "You can do anything that's legal."

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:

Create a Person and make it say hello


Create a student, make it say hello, set his age to 21, display his age and then print “I’m going to
class”.
Create a teacher, 30 years old, ask him to say hello and then explain a course.

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();

Student s = new Student();

Teacher t = new Teacher();


p.hello();
p=s;
p=t;
s.hello();
s.ShowAge();
s.GoToClasses();
t.hello();
t.ShowAge();
t.Explain();
t.setcourse("OOP");
}
}
Output:
ASSIGNMENT 1 Object Oriented Programming
BS(SE) 2019
NAME:ARMEEN JAVED
ENROLLMENT.NO:24308
DATE: 18-NOV-19

You might also like