JAVA Tutorial - 6
1
1. Student Details
Write a program to create a Student class with name, rollNo, and
marks variables.
Take input from the user using Scanner and display the details.
code import java.util.Scanner;
class Student {
String name;
int rollNo;
double marks;
void displayDetails() {
System.out.println("\n--- Student Details ---");
System.out.println("Name : " + name);
System.out.println("Roll No: " + rollNo);
System.out.println("Marks : " + marks);
}
}
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Student s = new Student();
System.out.print("Enter Name: ");
s.name = sc.nextLine();
System.out.print("Enter Roll No: ");
s.rollNo = sc.nextInt();
System.out.print("Enter Marks: ");
s.marks = sc.nextDouble();
s.displayDetails();
}
}
JAVA Tutorial - 6
o/p
2
2. Rectangle Area & Perimeter
Create a Rectangle class with length and width attributes.
Take input from the user and calculate area and perimeter using
methods.
code import java.util.Scanner;
class Rectangle {
double length;
double width;
double calculateArea()
{
return length * width;
}
double calculatePerimeter()
{
return 2 * (length + width);
}
}
class RectangleProgram
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
JAVA Tutorial - 6
Rectangle rect = new Rectangle();
System.out.print("Enter length: ");
rect.length = sc.nextDouble();
System.out.print("Enter width: ");
rect.width = sc.nextDouble();
System.out.println("\n--- Rectangle Details ---");
System.out.println("Length : " + rect.length);
System.out.println("Width : " + rect.width);
System.out.println("Area : " + rect.calculateArea());
System.out.println("Perimeter: " + rect.calculatePerimeter());
}
}
o/p
3
3. Bank Account
Write a program to create a BankAccount class with methods
deposit() and withdraw().
Use Scanner to take deposit/withdrawal amounts from the user and
display the balance.
code import java.util.Scanner;
class BankAccount
{
double balance;
BankAccount(double initialBalance)
{
JAVA Tutorial - 6
balance = initialBalance;
}
void deposit(double amount)
{
if (amount > 0)
{
balance += amount;
System.out.println("Deposited: " + amount);
}
else
{
System.out.println("Deposit amount must be positive!");
}
}
void withdraw(double amount)
{
if (amount > 0 && amount <= balance)
{
balance -= amount;
System.out.println("Withdrawn: " + amount);
}
else if (amount > balance)
{
System.out.println("Insufficient balance!");
}
else
{
System.out.println("Withdrawal amount must be positive!");
}
}
void displayBalance()
{
System.out.println("Current Balance: " + balance);
}
}
class BankAccountProgram
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter initial balance: ");
double initialBalance = sc.nextDouble();
BankAccount account = new BankAccount(initialBalance);
System.out.print("Enter deposit amount: ");
JAVA Tutorial - 6
double depositAmount = sc.nextDouble();
account.deposit(depositAmount);
account.displayBalance();
System.out.print("Enter withdrawal amount: ");
double withdrawAmount = sc.nextDouble();
account.withdraw(withdrawAmount);
account.displayBalance();
}
}
o/p
4
4. Book Information
Create a Book class with title, author, and price.
Take details of 3 books from the user and display them.
code import java.util.Scanner;
class Book
{
String title;
String author;
double price;
void setDetails(String title, String author, double price)
{
this.title = title;
this.author = author;
this.price = price;
}
JAVA Tutorial - 6
void displayDetails()
{
System.out.println("Title : " + title);
System.out.println("Author: " + author);
System.out.println("Price : " + price);
System.out.println("----------------------");
}
}
class BookInfoProgram
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
Book[] books = new Book[3];
for (int i = 0; i < books.length; i++)
{
books[i] = new Book();
System.out.println("\nEnter details for Book " + (i + 1) + ":");
System.out.print("Enter title: ");
String title = sc.nextLine();
System.out.print("Enter author: ");
String author = sc.nextLine();
System.out.print("Enter price: ");
double price = sc.nextDouble();
sc.nextLine();
books[i].setDetails(title, author, price);
}
System.out.println("\n--- Book Details ---");
for (Book b : books) {
b.displayDetails();
}
}
}
JAVA Tutorial - 6
o/p
5
5. Simple Calculator
Create a Calculator class with methods for addition, subtraction,
multiplication, and division.
Take two numbers and an operation choice from the user using
Scanner.
JAVA Tutorial - 6
code import java.util.Scanner;
class Calculator
{
double add(double a, double b)
{
return a + b;
}
double subtract(double a, double b)
{
return a - b;
}
double multiply(double a, double b)
{
return a * b;
}
double divide(double a, double b)
{
if (b != 0)
{
return a / b;
}
else
{
System.out.println("Error: Division by zero!");
return 0;
}
}
}
class CalculatorProgram
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
Calculator calc = new Calculator();
System.out.print("Enter first number: ");
double num1 = sc.nextDouble();
System.out.print("Enter second number: ");
double num2 = sc.nextDouble();
System.out.println("\nChoose an operation:");
System.out.println("1. Addition");
System.out.println("2. Subtraction");
JAVA Tutorial - 6
System.out.println("3. Multiplication");
System.out.println("4. Division");
System.out.print("Enter choice (1-4): ");
int choice = sc.nextInt();
double result = 0;
switch (choice)
{
case 1:
result = calc.add(num1, num2);
break;
case 2:
result = calc.subtract(num1, num2);
break;
case 3:
result = calc.multiply(num1, num2);
break;
case 4:
result = calc.divide(num1, num2);
break;
default:
System.out.println("Invalid choice!");
return;
}
System.out.println("Result: " + result);
}
}
JAVA Tutorial - 6
o/p
6
6. Circle Properties
Create a Circle class with a radius variable and methods to find area
JAVA Tutorial - 6
and circumference.
Take radius input from the user.
code import java.util.Scanner;
class Circle
{
double radius;
double calculateArea()
{
return Math.PI * radius * radius;
}
double calculateCircumference()
{
return 2 * Math.PI * radius;
}
}
class CircleProgram
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
Circle c = new Circle();
System.out.print("Enter radius of the circle: ");
c.radius = sc.nextDouble();
System.out.println("\n--- Circle Properties ---");
System.out.println("Radius : " + c.radius);
System.out.println("Area : " + c.calculateArea());
System.out.println("Circumference : " + c.calculateCircumference());
}
}
JAVA Tutorial - 6
o/p
7
7. Employee Salary
Write a program to create an Employee class with name, designation,
and salary.
Take input for 5 employees and display those whose salary is greater
than 50,000
code import java.util.Scanner;
class Employee
{
String name;
String designation;
double salary;
void setDetails(String name, String designation, double salary)
{
this.name = name;
this.designation = designation;
this.salary = salary;
}
void displayDetails()
{
System.out.println("Name : " + name);
System.out.println("Designation: " + designation);
System.out.println("Salary : " + salary);
System.out.println("----------------------");
}
}
JAVA Tutorial - 6
class EmployeeSalaryProgram
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
Employee[] employees = new Employee[5];
for (int i = 0; i < employees.length; i++)
{
employees[i] = new Employee();
System.out.println("\nEnter details for Employee " + (i + 1) + ":");
System.out.print("Enter name: ");
String name = sc.nextLine();
System.out.print("Enter designation: ");
String designation = sc.nextLine();
System.out.print("Enter salary: ");
double salary = sc.nextDouble();
sc.nextLine();
employees[i].setDetails(name, designation, salary);
}
System.out.println("\n--- Employees with Salary > 50000 ---");
boolean found = false;
for (Employee emp : employees)
{
if (emp.salary > 50000)
{
emp.displayDetails();
found = true;
}
}
if (!found)
{
System.out.println("No employees found with salary greater than
50000.");
}
}
}
JAVA Tutorial - 6
o/p
JAVA Tutorial - 6
8
8. Temperature Conversion
Create a Temperature class with methods to convert Celsius to
Fahrenheit and vice versa.
Take input from the user using Scanner.
code import java.util.Scanner;
class Temperature
{
double celsiusToFahrenheit(double celsius)
{
return (celsius * 9 / 5) + 32;
}
double fahrenheitToCelsius(double fahrenheit)
{
return (fahrenheit - 32) * 5 / 9;
}
}
class TemperatureProgram
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
Temperature temp = new Temperature();
System.out.println("Temperature Conversion");
System.out.println("1. Celsius to Fahrenheit");
System.out.println("2. Fahrenheit to Celsius");
System.out.print("Enter your choice (1 or 2): ");
int choice = sc.nextInt();
double result;
switch (choice)
{
case 1:
System.out.print("Enter temperature in Celsius: ");
double celsius = sc.nextDouble();
result = temp.celsiusToFahrenheit(celsius);
System.out.println("Fahrenheit: " + result);
break;
case 2:
System.out.print("Enter temperature in Fahrenheit: ");
double fahrenheit = sc.nextDouble();
JAVA Tutorial - 6
result = temp.fahrenheitToCelsius(fahrenheit);
System.out.println("Celsius: " + result);
break;
default:
System.out.println("Invalid choice!");
}
}
}
o/p
9
9. Even or Odd Checker
Create a class NumberCheck with a method isEven() that returns true
if the number is even.
Take a number from the user using Scanner and display the result.
code import java.util.Scanner;
class NumberCheck
{
boolean isEven(int num)
{
return num % 2 == 0;
JAVA Tutorial - 6
}
}
class NumberCheckProgram
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
NumberCheck checker = new NumberCheck();
System.out.print("Enter a number: ");
int number = sc.nextInt();
if (checker.isEven(number))
{
System.out.println(number + " is Even.");
}
else
{
System.out.println(number + " is Odd.");
}
}
}
o/p
10
10. Product Discount
Write a program to create a Product class with productName, price,
and a method to apply discount.
Take product details from the user and display price after discount.
code import java.util.Scanner;
class Product
JAVA Tutorial - 6
{
String productName;
double price;
double applyDiscount(double discountPercent)
{
if (discountPercent < 0 || discountPercent > 100)
{
System.out.println("Invalid discount percentage!");
return price;
}
double discountAmount = (price * discountPercent) / 100;
return price - discountAmount;
}
}
class ProductDiscountProgram
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
Product p = new Product();
System.out.print("Enter product name: ");
p.productName = sc.nextLine();
System.out.print("Enter product price: ");
p.price = sc.nextDouble();
System.out.print("Enter discount percentage: ");
double discountPercent = sc.nextDouble();
double finalPrice = p.applyDiscount(discountPercent);
System.out.println("\n--- Product Details ---");
System.out.println("Product Name : " + p.productName);
System.out.println("Original Price: " + p.price);
System.out.println("Discounted Price: " + finalPrice);
}
}
JAVA Tutorial - 6
o/p