0% found this document useful (0 votes)
3 views3 pages

Assignment 4 Java 57

The document outlines an assignment involving object-oriented programming with Java, focusing on creating classes and constructors. It includes tasks to create a Person class with default and parameterized constructors, as well as a Car class with various data types and methods. The document also provides sample code and expected outputs for each task, highlighting issues and corrections in the code.

Uploaded by

anishwarushe0725
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Assignment 4 Java 57

The document outlines an assignment involving object-oriented programming with Java, focusing on creating classes and constructors. It includes tasks to create a Person class with default and parameterized constructors, as well as a Car class with various data types and methods. The document also provides sample code and expected outputs for each task, highlighting issues and corrections in the code.

Uploaded by

anishwarushe0725
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

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

You might also like