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

Between 24 Jan to 28 Jan – Programs to Submit

Sarthak Tripathi, 21070122194, CS-C

Question 7: Create a class with default constructor.


Code:
public class seven {
int num;

public seven() {
num = 25;
}

public static void main(String[] args) {


seven newObject = new seven();
System.out.printf("The value of num set by the default constructor is %d.",
newObject.num);
}
}

Output:
Question 8: Add parameterized constructor.
Code:
public class eight {
int num;

public eight(int param) {


num = param;
}

public static void main(String[] args) {


eight newObject = new eight(30);
System.out.printf("The value of num set by the parameterized constructor is %d.",
newObject.num);
}
}

Output:
Question 9: Add copy constructor.
Code:
public class nine {
String name;

public nine(String param) {


name = param;
}

public nine(nine obj) {


name = obj.name;
}

public static void main(String[] args) {


nine firstObject = new nine("Sarthak");
System.out.printf("The value of name set by the parameterized constructor is %s.",
firstObject.name);

nine secondObject = new nine(firstObject);


System.out.printf("\nThe value of name set by the copy constructor is %s.",
secondObject.name);
}
}

Output:
Question 10: Create class Vehicle with required methods, parameters and
constructor.
Code:
public class Vehicle {
private String name, color;
private int cost;

// default constructor
public Vehicle() {
name = "None";
color = "None";
cost = 0;
}

// parameterized constructor
public Vehicle(String n, String col, int price) {
name = n;
color = col;
cost = price;
}

// copy constructor
public Vehicle(Vehicle obj) {
name = obj.name;
color = obj.color;
cost = obj.cost;
}

// method to set the name of the vehicle object


void setName(String n) {
name = n;
}

// method to set the color of the vehicle object


void setColor(String n) {
color = n;
}

// method to set the cost of the vehicle object


void setCost(int n) {
cost = n;
}

public static void main(String[] args) {


Vehicle emptyVehicle = new Vehicle();
System.out.printf("Default Constructor:\nName: %s,\nColor: %s,\nCost: %d\n", emptyVehicle.name,
emptyVehicle.color, emptyVehicle.cost);

emptyVehicle.setName("Car-1");
emptyVehicle.setColor("Red");
emptyVehicle.setCost(2000000);
System.out.printf("\nValues after setting property values using methods:\nName: %s,\nColor:
%s,\nCost: %d\n", emptyVehicle.name, emptyVehicle.color, emptyVehicle.cost);

Vehicle newVehicle = new Vehicle("Car-2", "White", 6000000);


System.out.printf("\nParameterized Constructor:\nName: %s,\nColor: %s,\nCost: %d\n",
newVehicle.name, newVehicle.color, newVehicle.cost);

Vehicle copyVehicle = new Vehicle(emptyVehicle);


System.out.printf("\nCopy Constructor (Copying Car-1):\nName: %s,\nColor: %s,\nCost: %d\n",
copyVehicle.name, copyVehicle.color, copyVehicle.cost);

copyVehicle.setName("Car-3");
copyVehicle.setColor("Black");
copyVehicle.setCost(4500000);
System.out.printf("\nValues of Copied Object after setting property values using methods:\nName:
%s,\nColor: %s,\nCost: %d\n", copyVehicle.name, copyVehicle.color, copyVehicle.cost);
}
}
Output:
Question 11: Demonstrate use of this keyword.
Code:
public class Fruit {
private String name;

Fruit(String name) {
this.name = name;
}

void showDiff(String name) {


System.out.printf("name: %s\nthis.name: %s", name, this.name);
}

public static void main(String[] args) {


Fruit newPerson = new Fruit("Apple");

newPerson.showDiff("Banana");
}
}

Output:
Question 12: WAP to demonstrate parameterized methods (call by value).
Code:
public class Person {
private Double weight;

// method to set the name of the vehicle object


void setWeight(Double w) {
weight = w;
}

public static void main(String[] args) {


Person newPerson = new Person();

newPerson.setWeight(62.4);
System.out.printf("The set weight of the person is %.2fkg.", newPerson.weight);
}
}

Output:
Question 13: Write a Person class with an instance variable and a constructor
that takes an integer as a parameter. The constructor must assign after
confirming that the argument passed is not negative. If a negative argument is
passed, the constructor should set the age to 0 and print "Age is not valid."
In addition, you must write the following instance methods:
• yearPasses(): should increase the instance variable by .
• amIOld(): should perform the following conditional actions:
̉• If age < 13, print "You are young."
• If age > 12 and age < 20, print "You are a teenager."
• Otherwise, print "You are old."
Code:
public class Person {
private int age;

// parameterized constructor
public Person(int n) {
if (n < 0) {
age = 0;
System.out.println("The entered age is invalid.");
return;
}

age = n;
}

// method to set the name of the vehicle object


void yearPasses() {
age++;
}

// method to set the color of the vehicle object


void amIOld() {
if (age < 13) {
System.out.println("You are young.");
} else if (age < 20) {
System.out.println("You are a teenager.");
} else {
System.out.println("You are old.");
}
}

public static void main(String[] args) {


Person newPerson = new Person(Integer.parseInt(args[0]));
System.out.printf("Your age is %d.\n", newPerson.age);
newPerson.amIOld();

newPerson.yearPasses();
System.out.printf("\nA year has passed. Your age is now %d.\n", newPerson.age);
newPerson.amIOld();
}
}

Output:

You might also like