Week 2 of OOP in JAVA
Create a class Complex with data members real and imaginary. Use a parameterized constructor to initialize the values. Create a method to add two complex numbers and display the result in the form a + bi. Create objects in the main() method and demonstrate addition.
class Complex{
double real;
double imaginary;
Complex(double a,double b){
real = a;
imaginary = b;
}
void addComplex(Complex z1, Complex z2){
System.out.printf("%.1f+%.1fi",z1.real+z2.real,z1.imaginary+z2.imaginary);
}
}
public class ComplexNo {
public static void main(String[] args){
Complex z1 = new Complex(12.4,5.6);
Complex z2 = new Complex(8.4,6.6);
z1.addComplex(z1,z2);
}
}
Create a class Date with data members day, month, and year. Use a constructor to initialize the date. Write a method to display the date in dd/mm/yyyy format. Validate whether the date entered is valid or not.
class Date{
int dd,mm,yyyy;
Date(int day,int month, int year){
if(year > 9999 || year < 0){
throw new IllegalArgumentException("The year must be between 0 and 9999");
}
if(month > 12 || month < 1){
throw new IllegalArgumentException("The month must be between 1 and 12");
}
int maxDay;
switch (month) {
case 2:
boolean leap = (year%4==0 && year%100 != 0)||(year%400==0);
maxDay = leap?29:28;
break;
case 1,3,5,7,8,10,12:
maxDay = 31;
break;
default:
maxDay = 30;
}
if(day > maxDay || day < 1){
throw new IllegalArgumentException("Enter a valid date for given month");
}
dd = day;
mm = month;
yyyy = year;
}
void displayDate(){
System.out.printf("%02d/%02d/%04d",dd,mm,yyyy);
}
}
public class DateViewer{
public static void main(String[] args) {
try {
Date date = new Date(4,3,746);
date.displayDate();
} catch (Exception e) {
System.out.println(e);
}
}
}
Create a class Vector3D with data members x, y, and z. Use a constructor to initialize vector components. Write a method to calculate the magnitude of the vector. Create two vector objects and display their magnitudes.
class Vector3D{
int x,y,z;
Vector3D(int a,int b,int c){
x = a;
y = b;
z = c;
}
void magnitude(){
double result;
result = Math.sqrt(x*x + y*y + z*z);
System.out.printf("The magnitude is %.2f\n",result);
}
}
public class Vector {
public static void main(String[] args) {
Vector3D v1 = new Vector3D(3, 4, 5);
Vector3D v2 = new Vector3D(6, 7, 3);
v1.magnitude();
v2.magnitude();
}
}
Create a class MyString that stores a string.Use a constructor to initialize the string.Write methods to find the length of the string and compare it with another string object.Display the results.
class MyString{
String text;
MyString(String input){
text = input;
}
void size(){
System.out.println("The length of the string is " + text.length());
}
void compare(MyString other){
int val = text.compareTo(other.text);
if(val > 0){
System.out.println(text + " is greater than " + other.text);
}
else if(val < 0){
System.out.println(text + " is lesser than " + other.text);
}
else{
System.out.println("Both are equal strings");
}
}
}
public class Text {
public static void main(String[] args) {
MyString name1 = new MyString("Suresh");
name1.size();
MyString name2 = new MyString("Harshit");
name2.size();
name1.compare(name2);
}
}
Create a class BankAccount with data members accountNumber, accountHolderName, and balance. Initialize the data using a constructor. Display account details. Create multiple objects to represent different customers.
class BankAccount{
String accountNumber,accountHolderName;
double balance;
BankAccount(String no,String name,double bal){
accountHolderName = name;
accountNumber = no;
balance = bal;
}
void details(){
System.out.println("Account Number = " + accountNumber);
System.out.println("Account Holder = " + accountHolderName);
System.out.println("Current Balance = " + balance);
}
}
public class Banking {
public static void main(String[] args) {
BankAccount one = new BankAccount("BN4521", "Rohan", 5000);
BankAccount two = new BankAccount("BN4411", "Ambani", 786000);
BankAccount three = new BankAccount("BN8221", "Mamta", 7410200);
BankAccount four = new BankAccount("BN3871", "Shivam", 3000);
one.details();
two.details();
three.details();
four.details();
}
}
Create a class Student with data members name, roll, and marks of 3 subjects. Use a constructor to initialize values. Write methods to calculate total and average marks. Display student details with result.
class Student{
String name;
int roll;
int phy,chem,math;
Student(String text,int no,int p,int c,int m){
name = text;
roll = no;
phy = p;
chem = c;
math = m;
}
int total(){
return phy+chem+math;
}
double average(){
return (phy+chem+math)/3;
}
void details(){
System.out.println("--------------------------------------");
System.out.println("Name = " + name);
System.out.println("Roll = " + roll);
System.out.println("--------------------------------------");
System.out.println("Physics | Chemistry | Maths");
System.out.printf("%8d|%11d|%5d\n",phy,chem,math);
System.out.println("--------------------------------------");
System.out.println("Total Marks = " + total());
System.out.println("Total Average = " + average());
}
}
public class StudentMain {
public static void main(String[] args) {
Student s1 = new Student("Arham",12,74,71,83);
s1.details();
}
}
Create a class Rectangle with data members length and breadth. Initialize values using constructor overloading (default and parameterized). Write a method to calculate area and perimeter. Create objects using both constructors.
class Rectangle{
double length, breadth;
Rectangle(){
length = 1;
breadth = 1;
}
Rectangle(double l,double b){
length = l;
breadth = b;
}
double area(){
return length*breadth;
}
double perimeter(){
return 2*(length+breadth);
}
}
public class Rect {
public static void main(String[] args) {
Rectangle r1 = new Rectangle();
Rectangle r2 = new Rectangle(4.6,3.9);
System.out.println(r1.area() + "sq.ft and " + r1.perimeter()+ "ft");
System.out.println(r2.area() + "sq.ft and " + r2.perimeter()+ "ft");
}
}
Create a class Employee with data members empId, name, and basicSalary. Initialize the details using constructor. Write a method to calculate gross salary (add HRA and DA). Display employee salary details.
class Employee{
String empId,name;
int basicSalary;
Employee(String id,String text,int sal){
empId = id;
name = text;
basicSalary = sal;
}
int grossSalary(int hra, int da){
return hra+da;
}
void salaryDetails(int hra, int da){
System.out.println("Basic Salary = " + basicSalary);
System.out.println("HRA = " + hra);
System.out.println("DA = " + da);
System.out.println("Gross Salary = " + grossSalary(hra, da));
}
}
public class Emp {
public static void main(String[] args) {
Employee e1 = new Employee("ETA4175", "Jeffery", 140000);
e1.salaryDetails(120000, 45000);
}
}
Create a class Time with data members hours, minutes, and seconds. Use a constructor to initialize time. Write a method to display time in proper format. Create two objects and display both times.
class Time{
int hours,minutes,seconds;
Time(){
hours = 0;
minutes = 0;
seconds = 0;
}
Time(int h,int m,int s){
if((h>-1 && h<24) && (m>-1 && m<60) && (s>-1 && s<60)){
hours = h;
minutes = m;
seconds = s;
}
else{
System.out.println("You have entered an invalid Time");
}
}
void displayTime(){
System.out.printf("%02d:%02d:%02d\n",hours,minutes,seconds);
}
}
public class Timeclass {
public static void main(String[] args) {
Time time1 = new Time();
Time time2 = new Time(23,13,58);
time1.displayTime();
time2.displayTime();
}
}
Create a class Book with data members title, author, and price. Initialize using constructor. Write a method to display book information. Create multiple book objects and display details.
class Book{
String title, author;
int price;
Book(String t, String a,int p){
title = t;
author = a;
price = p;
}
void bookInfo(){
System.out.println("Title of the Book : " + title);
System.out.println("Author of the Book : " + author);
System.out.println("Price of the Book : " + price);
System.out.println();
}
}
public class Books {
public static void main(String[] args) {
Book book1 = new Book("Ganit Prakash","Sanjay Palli",200);
Book book2 = new Book("Bliss","Sumitra Banerjee",30);
Book book3 = new Book("Bornoparichay","Vidyasagar",50);
book1.bookInfo();
book2.bookInfo();
book3.bookInfo();
}
}
Comments