Uploaded by PRADISH G

assessement2

advertisement
Write a java program to demonstrate static variables and methods.
class StaticDemo {
static int count = 0;
void incrementCount() {
count++;
}
static void printCount() {
System.out.println("Count: " + count);
}
public static void main(String[] args) {
StaticDemo obj1 = new StaticDemo();
obj1.incrementCount();
StaticDemo obj2 = new StaticDemo();
obj2.incrementCount();
StaticDemo.printCount();
}
}
2. Write a java program to give the example for ‘this’ keyword.
class ThisDemo {
void print() {
System.out.println("This is a ThisDemo object");
}
public static void main(String[] args) {
ThisDemo obj = new ThisDemo();
obj.print();
}
}
2. Define a class called Student with the following attributes:
Roll number
Name
Date of birth
Weight
Height
Mark
Write a suitable constructor and a method to display the details of a Student object. In
the main method create some student objects and display the roll number and the name
of the students who are 19 years old or more with weight above 90.5 kg but height less
than 175.0 cm.
import java.util.Date;
class Student {
private int rollNumber;
private String name;
private Date dateOfBirth;
private double weight;
private double height;
private int mark;
public Student(int rollNumber, String name, Date dateOfBirth, double weight,
double height, int mark) {
this.rollNumber = rollNumber;
this.name = name;
this.dateOfBirth = dateOfBirth;
this.weight = weight;
this.height = height;
this.mark = mark;
}
public void displayDetails() {
System.out.println("Roll number: " + rollNumber);
System.out.println("Name: " + name);
System.out.println("Date of birth: " + dateOfBirth);
System.out.println("Weight: " + weight);
System.out.println("Height: " + height);
System.out.println("Mark: " + mark);
}
public boolean isEligible() {
return dateOfBirth.getYear() >= 1999 && weight > 90.5 && height < 175.0;
}
}
public class Main {
public static void main(String[] args) {
Student student1 = new Student(1, "John Doe", new Date(2000, 1, 1), 80.5, 180.0,
90);
Student student2 = new Student(2, "Jane Doe", new Date(1999, 2, 2), 60.0, 165.0,
85);
Student student3 = new Student(3, "Peter Smith", new Date(1998, 3, 3), 95.0, 170.0,
70);
List<Student> students = Arrays.asList(student1, student2, student3);
for (Student student : students) {
if (student.isEligible()) {
System.out.println("Roll number: " + student.getRollNumber());
System.out.println("Name: " + student.getName());
}
}
}
}
3. Define a class called Car with the following attributes:
Engine number
Registration number
Date of registration
Colour of the car
Number of seats
Engine capacity
Brand of the car
Model of the car
Owner of the car
In the above attributes, the owner attribute is an object of the Owner class which has the
following attributes:
Name of the owner
Street
Town
State
Pincode
Date of birth
Profession
Driving license number
Define the Owner class and create an array own[] of owner objects displayOwner(). Write a
method displayOwner() for displaying the details of an owner object in a brief form and use
it to print all the elements of the array of owner objects. In the Car class create an array
own[] of five owner objects. Also create an array of five Car objects. Define in such a way
that own[] is the owner of car[], own[] is the owner of car[1] and so on. Define a method for
displaying the details of a car. This method must use of the displayOwner() method of the
Owner class.
import java.util.Date;
class Owner {
String name;
String street;
String town;
String state;
int pincode;
Date dateOfBirth;
String profession;
String drivingLicenseNumber;
public Owner(String name, String street, String town, String state, int pincode, Date
dateOfBirth, String profession, String drivingLicenseNumber) {
this.name = name;
this.street = street;
this.town = town;
this.state = state;
this.pincode = pincode;
this.dateOfBirth = dateOfBirth;
this.profession = profession;
this.drivingLicenseNumber = drivingLicenseNumber;
}
public void displayOwner() {
System.out.println("Name: " + name);
System.out.println("Street: " + street);
System.out.println("Town: " + town);
System.out.println("State: " + state);
System.out.println("Pincode: " + pincode);
System.out.println("Date of Birth: " + dateOfBirth);
System.out.println("Profession: " + profession);
System.out.println("Driving License Number: " + drivingLicenseNumber);
}
}
class Car {
String engineNumber;
String registrationNumber;
Date dateOfRegistration;
String colour;
int numberOfSeats;
int engineCapacity;
String brand;
String model;
Owner owner;
public Car(String engineNumber, String registrationNumber, Date dateOfRegistration, String
colour, int numberOfSeats, int engineCapacity, String brand, String model, Owner owner) {
this.engineNumber = engineNumber;
this.registrationNumber = registrationNumber;
this.dateOfRegistration = dateOfRegistration;
this.colour = colour;
this.numberOfSeats = numberOfSeats;
this.engineCapacity = engineCapacity;
this.brand = brand;
this.model = model;
this.owner = owner;
}
public void displayCar() {
System.out.println("Engine Number: " + engineNumber);
System.out.println("Registration Number: " + registrationNumber);
System.out.println("Date of Registration: " + dateOfRegistration);
System.out.println("Colour: " + colour);
System.out.println("Number of Seats: " + numberOfSeats);
System.out.println("Engine Capacity: " + engineCapacity);
System.out.println("Brand: " + brand);
System.out.println("Model: " + model);
owner.displayOwner();
}
}
public class Main {
public static void main(String[] args) {
Owner[] owners = new Owner[5];
Car[] cars = new Car[5];
for (int i = 0; i < 5; i++) {
owners[i] = new Owner("John Doe", "123 Main Street", "Anytown", "CA", 12345, new
Date(), "Software Engineer", "1234567890");
cars[i] = new Car("1234567890", "ABC123", new Date(), "Red", 5, 1200, "Toyota",
"Camry", owners[i]);
}
for (int i = 0; i < 5; i++) {
cars[i].displayCar();
}
}
}
Download