JAVA JOURNAL
Name: _____________________________
Roll No: __________________________
Subject: JAVA (DSC2)
Semester: 5th ([Link] IT)
College: __________________________
Program 1
Write a Java program that tests a number as input & prints its
multiplication table.
Code:
import [Link];
class MultiplicationTable {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int n = [Link]();
[Link]("Multiplication Table of " + n + ":");
for(int i=1; i<=10; i++) {
[Link](n + " x " + i + " = " + (n*i));
}
[Link]();
}
}
Sample Output:
Enter a number: 5
Multiplication Table of 5:
5 x 1 = 5
5 x 2 = 10
...
5 x 10 = 50
Program 2
Write a Java program that tests a year from the user & prints whether it
is a leap year or not.
Code:
import [Link];
class LeapYear {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a year: ");
int year = [Link]();
if((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) {
[Link](year + " is a Leap Year.");
} else {
[Link](year + " is not a Leap Year.");
}
[Link]();
}
}
Sample Output:
Enter a year: 2024
2024 is a Leap Year.
Program 3
Write a Java program to take input from user & display whether it is an
Armstrong number or not.
Code:
import [Link];
class Armstrong {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
int original = num, sum = 0;
while(num > 0) {
int digit = num % 10;
sum += digit*digit*digit;
num /= 10;
}
if(sum == original)
[Link](original + " is an Armstrong number.");
else
[Link](original + " is not an Armstrong
number.");
[Link]();
}
}
Sample Output:
Enter a number: 153
153 is an Armstrong number.
Program 4
Write a Java program to display pattern like a right angle triangle with
a number up to 10.
Code:
class NumberTriangle {
public static void main(String[] args) {
for(int i=1; i<=10; i++) {
for(int j=1; j<=i; j++) {
[Link](j + " ");
}
[Link]();
}
}
}
Sample Output:
1
1 2
1 2 3
...
1 2 3 4 5 6 7 8 9 10
Program 5
Write a Java program to create a class called Book with instance
variables title, author and price. Implement default constructor, and
two parameterized constructors.
Code:
class Book {
String title, author;
double price;
Book() {
title = "Unknown";
author = "Unknown";
price = 0.0;
}
Book(String t, String a) {
title = t; author = a; price = 0.0;
}
Book(String t, String a, double p) {
title = t; author = a; price = p;
}
void display() {
[Link]("Title: " + title + ", Author: " + author +
", Price: " + price);
}
public static void main(String[] args) {
Book b1 = new Book();
Book b2 = new Book("Java Basics", "James Gosling");
Book b3 = new Book("OOP in Java", "Herbert Schildt", 500);
[Link]();
[Link]();
[Link]();
}
}
Sample Output:
Title: Unknown, Author: Unknown, Price: 0.0
Title: Java Basics, Author: James Gosling, Price: 0.0
Title: OOP in Java, Author: Herbert Schildt, Price: 500.0
Program 6
Write a Java program to create a class called Shape with a method
getArea. Create a subclass Rectangle that overrides getArea method to
calculate the area of rectangle.
Code:
class Shape {
double getArea() { return 0; }
}
class Rectangle extends Shape {
double length, breadth;
Rectangle(double l, double b) {
length = l; breadth = b;
}
double getArea() {
return length * breadth;
}
public static void main(String[] args) {
Rectangle r = new Rectangle(5, 10);
[Link]("Area of Rectangle: " + [Link]());
}
}
Sample Output:
Area of Rectangle: 50.0
Program 7
Write a Java program to create a class known as BankAccount with methods
deposit & withdraw. Create a subclass SavingsAccount that overrides the
withdraw method to prevent balance falling below 100.
Code:
class BankAccount {
double balance;
BankAccount(double b) { balance = b; }
void deposit(double amt) { balance += amt; }
void withdraw(double amt) { balance -= amt; }
}
class SavingsAccount extends BankAccount {
SavingsAccount(double b) { super(b); }
void withdraw(double amt) {
if(balance - amt < 100)
[Link]("Withdrawal denied! Minimum balance 100
required.");
else
balance -= amt;
}
public static void main(String[] args) {
SavingsAccount sa = new SavingsAccount(500);
[Link](450);
[Link]("Balance: " + [Link]);
[Link](200);
[Link]("Balance: " + [Link]);
}
}
Sample Output:
Withdrawal denied! Minimum balance 100 required.
Balance: 500.0
Balance: 300.0
Program 8
Write a Java program to create an interface Flyable with method
flyObject. Create three classes Spacecraft, Airplane, Helicopter that
implement Flyable interface.
Code:
interface Flyable {
void flyObject();
}
class Spacecraft implements Flyable {
public void flyObject() { [Link]("Spacecraft is flying
in space."); }
}
class Airplane implements Flyable {
public void flyObject() { [Link]("Airplane is flying in
the sky."); }
}
class Helicopter implements Flyable {
public void flyObject() { [Link]("Helicopter is flying
at low altitude."); }
}
class TestFly {
public static void main(String[] args) {
Flyable f1 = new Spacecraft();
Flyable f2 = new Airplane();
Flyable f3 = new Helicopter();
[Link]();
[Link]();
[Link]();
}
}
Sample Output:
Spacecraft is flying in space.
Airplane is flying in the sky.
Helicopter is flying at low altitude.
Program 9
Write a Java program to demonstrate method overloading.
Code:
class OverloadDemo {
void display(int a) { [Link]("Integer: " + a); }
void display(double b) { [Link]("Double: " + b); }
void display(String s) { [Link]("String: " + s); }
public static void main(String[] args) {
OverloadDemo obj = new OverloadDemo();
[Link](10);
[Link](5.5);
[Link]("Hello");
}
}
Sample Output:
Integer: 10
Double: 5.5
String: Hello
Program 10
Write a Java program.
Code:
class HelloWorld {
public static void main(String[] args) {
[Link]("Hello, Java Journal!");
}
}
Sample Output:
Hello, Java Journal!