ASSIGNMENT NO.
OBJECT ORIENTED PROGRAMMING
NAME-SHREEYA JOSHI
ROLLNO.-57
PRN-2122000723
Create two classes Person and Demo Person. Run and Observe the output of this
code.
class person{
String name;
int age;
public void talk(){
System.out.println("Hello i am " +name);
System.out.println("my age is " +age);
}// NAME=NULL
// AGE =0
}
class demoperson{
public static void main(String[] args){
person james=new person();
james.talk();
}
}
Output is :-Hello i am null
my age is 0
Q2 Add Default Constructor to the Persons Class using below
code and Test the Code .
Identify what is wrong in the Output of this code.
class person{
String name;
int age;
person(){
name="Parth";
age=20;
}
public void talk(){
System.out.println("hello i am "+name);
System.out.println("my age is"+age);
}
}
class demoperson{
public static void main(String[] args){
person r=new person();
r.talk();
person emma =new person();// in this code error so crate a
new object on person and call the function talk and print the
two times of this code
emma.talk();
}
}
Output;- hello i am Parth
my age is20
hello i am Parth
my age is20
Q3Now to Fix the Issue in the output , add parameterized
Constructor for the Person class
class person{
String name;
int age;
person(){
name="Parth";
age=20;
}
person(String s,int i){
name = s;
age =i;
}
public void talk(){
System.out.println("hello i am "+name);
System.out.println("my age is"+age);
}
}
class demoperson{
public static void main(String[] args){
person r=new person();
r.talk();
person emma =new person("emma",25);// in this code
error so crate a new object on person and passing
parameterized argument in the new name and age so call the
first and last output
emma.talk();
}
}
Output is:- hello i am Parth
my age is20
hello i am emma
my age is25
Q4
4. Create Class Car. Add below DataTypes to the Car Class. (Class
Variables and not variables in main method) byte, short , int,
long , float, char, String
Create Method showCarDetails() to print the Car Class
Attributes. (Similar to talk() method in above examples )
1. Add Main Method
2. Create Object Car Maruti = new Car();
3. Print hashcode of Maruti Object using below line :
System.out.println("Object Hashcode is "+Maruti.hashcode());
4. Maruti. showCarDetails();
5. List down all the default values assigned to Car Class by JVM.
class car{
String name;
int hashcode;
car(){
name="maruti";
hashcode=6299;
}
public void showcardetails(){
System.out.println("name is:"+name.hashcode());
}
public static void main(String[] args){
car maruti=new car();
maruti.showcardetails();
}
}
Output:- maruti6299