KEMBAR78
Java Programs Hemanth Reddy | PDF | Integer (Computer Science) | Constructor (Object Oriented Programming)
0% found this document useful (0 votes)
3 views7 pages

Java Programs Hemanth Reddy

Uploaded by

ravihari767687
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views7 pages

Java Programs Hemanth Reddy

Uploaded by

ravihari767687
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

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() {
System.out.println("Book Code : " + code);
System.out.println("Book Title: " + title);
System.out.println("Book Price: " + price);
}

public static void main(String[] args) {


Book b1 = new Book();
b1.setBook(101, "Java Programming", 450.50);
b1.display();
}
}

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 java.util.Scanner;

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(System.in);
System.out.print("Enter first number: ");
int x = sc.nextInt();
System.out.print("Enter second number: ");
int y = sc.nextInt();

Arithmetic obj = new Arithmetic();


System.out.println("Addition: " + obj.add(x, y));
System.out.println("Subtraction: " + obj.subtract(x, y));
System.out.println("Multiplication: " + obj.multiply(x, y));
System.out.println("Division: " + obj.divide(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) {
System.out.println("Greater integer: " + (a > b ? a : b));
}

void compare(char a, char b) {


System.out.println("Greater character: " + (a > b ? a : b));
}

void compare(String a, String b) {


System.out.println("Greater string: " + (a.compareTo(b) > 0 ? a : b));
}

public static void main(String[] args) {


CompareDemo obj = new CompareDemo();
obj.compare(10, 20);
obj.compare('x', 'a');
obj.compare("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() {
System.out.println("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);

s1.display();
s2.display();
s3.display();
}
}

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 {
System.out.println("Insufficient balance!");
}
}

void displayBalance() {
System.out.println("Current Balance: " + balance);
}

public static void main(String[] args) {


Account acc = new Account();
acc.startAccount(1000);
acc.deposit(500);
acc.withdraw(300);
acc.displayBalance();
}
}

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() {
System.out.println("Book Code : " + code);
System.out.println("Book Title: " + title);
System.out.println("Book Price: " + price);
}

public static void main(String[] args) {


Book b1 = new Book(201, "C Programming", 350.75);
b1.display();
}
}

Sample Output:
Book Code : 201
Book Title: C Programming
Book Price: 350.75

You might also like