CSE 12 Batch Question Solve 1
CSE 12 Batch Question Solve 1
// Method
void honk() {
System.out.println(brand + " is honking!");
}
}
// Setting attributes
myVehicle.color = "Red";
myVehicle.brand = "Toyota";
myVehicle.speed = 120;
// Printing two attributes
System.out.println("Vehicle Brand: " + myVehicle.brand);
System.out.println("Vehicle Color: " + myVehicle.color);
// Calling a method
myVehicle.honk();
}
}
(c) Define string class
In Java, the String class is part of the java.lang package and is used to represent a
sequence of characters. Strings are widely used in Java for storing and
manipulating text.
3. (a) create an array containing 5 elements of integer data. read their values using
scanner class and print them as twice (double of the assigned values).
Ans:
import java.util.Scanner;
5a. Define Constructor. Why do we use 'get' and 'set' methods in Java?
Definition of Constructor:
A constructor is a special method in Java used to initialize objects. It is called
automatically when an object of a class is created.
The get method returns the variable value, and the set method sets the value.
import java.util.Scanner;
sc.close();
}
}
Enter first integer: 10
Enter second integer: 4
Result of subtraction: 6
// Superclass
class Vehicle {
String brand;
// Method in Superclass
public void displayBrand() {
System.out.println("Brand: " + brand);
}
}
// Subclass
class Car extends Vehicle {
int speed;