CTOOD
CTOOD
Q1. Write a method isEven() that uses the remainder operator (%) to determine
whether an integer is even.The method should take an integer argument and return
true if the integer is even and false otherwise.1ncorporate this method into an
application that inputs a sequence of integers (one at a time) and determines whether
cach is even or odd.
→
import java.util.Scanner;
while (true) {
System.out.print("Enter an integer (or 'q' to quit): ");
if (input.hasNextInt()) {
int num = input.nextInt();
System.out.println(num + " is " + (isEven(num) ? "even" : "odd"));
} else {
String quit = input.next();
if (quit.equalsIgnoreCase("q")) {
System.out.println("Goodbye!");
break;
} else {
System.out.println("Invalid input, please try again.");
}
}
}
input.close();
}
}
scanner.close();
}
}
Q3. Create a Cuboid class with 3 public instance variables length, breadth and height
of type double,and a method volume (0. Create choice 2 objects with different values
obtained by command line arguments andprint the volume ofeach. (The program must
take 6 values asinput).
→
import java.util.Scanner;
public class Cuboid {
Q4. What is an array in java? Whatare the types of anarray? How to declare and use
arrays?
→An array in Java is a data structure that stores a fixed-size, ordered collection of
elements of the
same data type. An array is declared using a square bracket notation [ ] and the type
of the
elements it will store.
Two types of array are :
1. One-dimensional arrays: An array that stores elements in a single row or column.
To retrieve the value of the third element in the array : int x = myArray[2];
Q5. Create class SavingsAccount. Use a static variable annual Interest Rate to store
the annualinterest rate for all account holders. Each object ofthe class contains a
private instance variable savings Balance indicating the amount the saver currently
has on deposit. Provide method calculate Monthly lnterest to calculate the monthly
interest by multiplying the savingsBalance by annualInterestRate divided by 12-this
SavingsAccount.modifyInterestRate(0.05);
saver1.calculateMonthlyInterest();
saver2.calculateMonthlyInterest();
System.out.println("Saver 1 balance after 2 months: "+saver1.getSavingsBalance());
System.out.println("Saver 2 balance after 2 months: "+saver2.getSavingsBalance());
}
}
→ Class Diagram:
Q8. What is constructor and what are the different types of constructors?
→ In OOPS construct is a method use to initialize object of a class the name of the
class and constructor is always same.
Private Constructor→ Private constructors are used in class that contains only
static members. The private constructor is always declared by using
a private keyword.
Q9. Create a class 'Student' with three data memberswhich are name, ageand
address. The constructorof the class assigns default vaues name as "unknown", age
as '0' and address as "not
available". It has two memberswith the same name 'setlnfo'. First choice method has
two parameters for name and age and assigns the same where as the second
method takes has three parameters which are assigned to name, age and address
respectively. Print the name,
age and address of 5 students.
public Student() {
[this.name](http://this.name/) = "unknown";
this.age = 0;
this.address = "not available";
}
System.out.println();
System.out.println("Rectangle 2:");
System.out.println("Width: " + r2.getWidth());
System.out.println("Height: " + r2.getHeight());
System.out.println("Area: " + r2.getArea());
System.out.println("Perimeter: " + r2.getPerimeter());
}
}
Q12. Design a class named Triangle that extends GeometricObject. The class
contains: Three double data fields named sidel, side2, and side3 with default values I
.0 to denote three sides of the triangle. A no-arg constructor that creates a default
triangle.
> A constructor that creates a triangle with the specified sidel , side2,
and side3. The accessor methods for all three data fields.
> A method named getArea() that returns the area of this triangle.
→ Class Diagram: