Java Programming Exercises
Name: CH Hemanth Reddy
Registration No: 2024036173
1.) Write a program to implement a book class that stores the
details of a book such as its code, title of the book and price.
class Book {
int code;
String title;
double price;
void setBook(int c, String t, double p) {
code = c;
title = t;
price = p;
}
void display() {
[Link]("Book Code : " + code);
[Link]("Book Title: " + title);
[Link]("Book Price: " + price);
}
public static void main(String[] args) {
Book b1 = new Book();
[Link](101, "Java Programming", 450.50);
[Link]();
}
}
Sample Output:
Book Code : 101
Book Title: Java Programming
Book Price: 450.5
2.) Write a program to read two numbers and perform arithmetic
operations using methods.
import [Link];
class Arithmetic {
int add(int a, int b) { return a + b; }
int subtract(int a, int b) { return a - b; }
int multiply(int a, int b) { return a * b; }
double divide(int a, int b) { return (double)a / b; }
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter first number: ");
int x = [Link]();
[Link]("Enter second number: ");
int y = [Link]();
Arithmetic obj = new Arithmetic();
[Link]("Addition: " + [Link](x, y));
[Link]("Subtraction: " + [Link](x, y));
[Link]("Multiplication: " + [Link](x, y));
[Link]("Division: " + [Link](x, y));
}
}
Sample Output:
Enter first number: 10
Enter second number: 5
Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2.0
1.) Design a class to overload a method compare to return the
greater of two numbers.
class CompareDemo {
void compare(int a, int b) {
[Link]("Greater integer: " + (a > b ? a : b));
}
void compare(char a, char b) {
[Link]("Greater character: " + (a > b ? a : b));
}
void compare(String a, String b) {
[Link]("Greater string: " + ([Link](b) > 0 ? a : b));
}
public static void main(String[] args) {
CompareDemo obj = new CompareDemo();
[Link](10, 20);
[Link]('x', 'a');
[Link]("apple", "banana");
}
}
Sample Output:
Greater integer: 20
Greater character: x
Greater string: banana
2.) Design a program which demonstrates the overload
constructors (using 3 constructors).
class Student {
int rollNo;
String name;
double marks;
Student() {
rollNo = 0;
name = "Unknown";
marks = 0.0;
}
Student(int r, String n) {
rollNo = r;
name = n;
marks = 0.0;
}
Student(int r, String n, double m) {
rollNo = r;
name = n;
marks = m;
}
void display() {
[Link]("Roll No: " + rollNo + ", Name: " + name + ", Marks: " + marks);
}
public static void main(String[] args) {
Student s1 = new Student();
Student s2 = new Student(101, "Ravi");
Student s3 = new Student(102, "Hari", 88.5);
[Link]();
[Link]();
[Link]();
}
}
Sample Output:
Roll No: 0, Name: Unknown, Marks: 0.0
Roll No: 101, Name: Ravi, Marks: 0.0
Roll No: 102, Name: Hari, Marks: 88.5
1.) Write a java program that creates a class account that stores a
variable balance and the class has methods to start Account, to
deposit money, to withdraw money and tell the current balance.
class Account {
double balance;
void startAccount(double amount) {
balance = amount;
}
void deposit(double amount) {
balance += amount;
}
void withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
} else {
[Link]("Insufficient balance!");
}
}
void displayBalance() {
[Link]("Current Balance: " + balance);
}
public static void main(String[] args) {
Account acc = new Account();
[Link](1000);
[Link](500);
[Link](300);
[Link]();
}
}
Sample Output:
Current Balance: 1200.0
2.) Write a java program to implement a class book program that
store the details of a book such a code, title, price using only
constructors.
class Book {
int code;
String title;
double price;
Book(int c, String t, double p) {
code = c;
title = t;
price = p;
}
void display() {
[Link]("Book Code : " + code);
[Link]("Book Title: " + title);
[Link]("Book Price: " + price);
}
public static void main(String[] args) {
Book b1 = new Book(201, "C Programming", 350.75);
[Link]();
}
}
Sample Output:
Book Code : 201
Book Title: C Programming
Book Price: 350.75