Assignment 4 Java 57
Assignment 4 Java 57
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
}
}
Output;- hello i am Parth
my age is20
hello i am Parth
my age is20
}
}
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