Java Training Program
Java Training Program
Training Program
Program 1-Welcome
• import java.util.Scanner;
• import java.util.Scanner;
public class scanner {
public class scanner1 {
• public static void main(String[]
args) { public static void main(String[]
• Scanner doc = new
Scanner(System.in); args) {
• float a = doc.nextFloat(); Scanner doc = new
• System.out.print(a);
• } Scanner(System.in);
• } float a = doc.nextFloat();
import java.util.Scanner;
int b = doc.nextInt();
public class scanner2 System.out.print(a+b);
{
public static void main(String[] args) }
{ }
Scanner doc = new Scanner(System.in);
String a = doc.nextLine();
System.out.print(a);
}
Program 5 – Get Input Variable for Personal Details (name, Age and Address)
• import java.util.Scanner;
•
public class scanner3
• {
• public static void main(String[] args)
• {
• Scanner Personal = new Scanner(System.in);
• String Name = Personal.nextLine();
• int Age = Personal.nextInt();
• Personal.nextLine();
• String Address = Personal.nextLine();
• System.out.println("My name is " + Name);
• System.out.println("Age is" + Age);
• System.out.print("Address is" + Address);
• }
• }
•• Program 6 – Get Input Variable for all your subject marks and find its total and average
••
••
••
••
••
•
•
•
•
•
•
•
••Program 6 – Get Input Variable for all your subject marks and find its total and average in decimal
••
••
••
••
••
•
•
•
•
•
•
((English+Tamil+Maths+Science+EVS)/5)
+
•• " %");
Program 7 – if statement
• public class ifelse
public class ifelse1
• {
{
• public static void main(String[]
args) public static void main(String[]
• { args)
• boolean vidamuyarchi = true; {
• if (vidamuyarchi) boolean vidamuyarchi =
• { false;
• System.out.print("Watch if (vidamuyarchi)
FDFS"); {
• } System.out.print("Watch
• } FDFS");
• } }
}
}
System o/p – Watch FDFS System o/p – No o/p
Program 8 – if else statement
import java.util.Scanner;
public class stringcompare1 {
Get input for the vijay’s next movie and public static void main(String[] args) {
comapre with his movie name. Scanner VNM = new Scanner(System.in);
If it is correct say
“You are right”
wrong then say
System.out.print("What is the name of
“you are wrong Vijay's next movie = ");
String a = VNM.nextLine();
String vijayNextMovie = "Jananayagam";
if (a.equals(vijayNextMovie))
{
System.out.print("You are Right");
}
else {
System.out.print("You are Wrong");
}
VNM.close();
} }
Operator – (Comparision ==, !=, <, >, <=, >=)
Logical operators in Java are used to perform logical operations on boolean expressions. The most
common logical operators are:
1.AND (&&): Returns true if both conditions are true, otherwise returns false.
•Example: if (a > 0 && b > 0)
•This condition will be true if both a and b are greater than 0.
2.OR (||): Returns true if at least one of the conditions is true, otherwise returns false.
•Example: if (a > 0 || b > 0)
•This condition will be true if either a or b is greater than 0.
3.NOT (!): Reverses the logical state of its operand. If the condition is true, it becomes false, and vice
versa.
•Example: if (!(a > 0))
•This condition will be true if a is not greater than 0.
Program 12 – Logical Operator(Logical &&, ||, !)
Problem Statement: A bank offers loans only to applicants who meet both of the following criteria:
•Their credit score is greater than or equal to 700.
•They have a stable monthly income above 30000.
The system should prompt the user to input their transaction/year amount and display the corresponding
credit limit.
Program 14 – else if statement
import java.util.Scanner;
public class ifelse3 {
public static void main(String[] args) {
Scanner credit = new Scanner(System.in);
System.out.print("Enter the customer transaction/year amount: ");
int amount = credit.nextInt();
int creditlimit;
import java.util.Scanner;
else if (gpa >= 6.0) {
public class ScholarshipEligibility { if (hasFinancialNeed) {
System.out.println("You are eligible for a
public static void main(String[] args) { partial scholarship based on financial need.");
Scanner scholar = new Scanner(System.in); } else {
System.out.println("You are not eligible for a
System.out.print("Enter your GPA: ");
double gpa = scholar.nextDouble();
scholarship.");
}
System.out.print("Do you have financial need }
(true/false)? "); else {
boolean hasFinancialNeed = scholar.nextBoolean(); System.out.println("Sorry, you are not eligible
for a scholarship.");
if (gpa >= 8.5) { }
if (hasFinancialNeed) {
System.out.println("Congratulations! You are
eligible for a full scholarship.");
scholar.close();
} else { }
System.out.println("Congratulations! You are }
eligible for a merit-based scholarship.");
}
}
Program 15 – Switch
import java.util.Scanner;
public class GradingSystem {
public static void main(String[] args) {
Scanner marks = new Scanner(System.in);
You are building a grading system for a school. The system
System.out.print("Enter the student's score: ");
needs to assign a grade to a student based on their score in an int score = marks.nextInt();
exam. The grading scheme is as follows:
char grade; // Calculate grade using switch-case
switch (score / 10) {
•Score 90 and above → Grade: A case 10: // Score between 90 to 100
•Score between 80 and 89 → Grade: B case 9: // Score between 90 to 99
grade = 'A';
•Score between 70 and 79 → Grade: C break;
•Score between 60 and 69 → Grade: D case 8: // Score between 80 to 89
•Score below 60 → Grade: F grade = 'B';
break;
case 7: // Score between 70 to 79
The goal is to write a Java program that takes the score as input grade = 'C';
break;
and prints the corresponding grade. case 6: // Score between 60 to 69
grade = 'D';
break;
default: // Score below 60
grade = 'F';
break;
}
System.out.println("The grade is: " + grade);
} }
Program 16 -Switch
Calculate the Shipping Cost Based on Package Type and Destination
You are building a simple application for an online e-commerce platform. The platform offers different types of shipping packages for customers,
and the shipping cost is based on the package type (Standard, Express, or Overnight) and the destination region (Local, National, or
International).
Objective:
Create a Java program using a switch statement to calculate the shipping cost based on the package type and destination.
Requirements:
•There are three types of shipping packages:
•Standard: Takes 5-7 days for delivery.
•Express: Takes 2-3 days for delivery.
•Overnight: Delivers within 24 hours.
•The destination regions are:
•Local: Within the same city or region.
•National: Within the same country but different cities.
•International: Shipping to other countries.
Shipping Cost Calculation: Sample Input and Output:
•Standard Shipping: Input :
•Local: Rs.50 •Package Type: Express
•National: Rs.100 •Destination: National
•International: Rs.350 Output: The shipping cost for Express shipping to National
•Express Shipping: destination is Rs.350.
•Local: Rs.150
•National: Rs.350
•International: Rs.500
•Overnight Shipping:
•Local: Rs.250
•National: Rs.750
•International: Rs.1550
int cost = 0;
switch(packageType) { case "overnight":
import java.util.Scanner; switch(destination) {
case "standard":
public class switch3 { switch(destination) {
case "local":
cost = 30;
public static void main(String[] case "local":
break;
cost = 5;
args) { break;
case "national":
cost = 50;
Scanner scanner = new case "national":
break;
cost = 10;
Scanner(System.in); break;
case "international":
cost = 100;
case "international":
break;
cost = 20;
System.out.print("Enter package break;
default:
System.out.println("Invalid
type (Standard, Express, default:
destination.");
System.out.println("Invalid destination.");
Overnight): "); return;
return;
}
String packageType = }
break;
break;
package.nextLine().toLowerCase( case "express":
default:
System.out.println("Invalid package type.");
); switch(destination) { return;
case "local": }
cost = 15;
System.out.print("Enter break;
System.out.println("The shipping cost for "
case "national":
destination (Local, National, cost = 25; + packageType + " shipping to " +
International): "); break; destination + " destination is Rs" + cost +
case "international":
String destination = cost = 40;
".");
package.nextLine().toLowerCase( break; scanner.close();
default: }
) System.out.println("Invalid destination."); }
return;
}
break;
Program 16 – for loop
Output
Iteration number: 1
Iteration number: 2
Iteration number: 3
Iteration number: 4
Iteration number: 5
Program 17 – for loop
Write a Java program that calculates the sum of all integers from 1 to a given number n using a for loop.
The program should take n as input from the user and then print the sum of all numbers from 1 to n.
import java.util.Scanner;
public class SumOfNumbers {
sum += i; can be read as:
public static void main(String[] args) {
Add the value of i to the current value
Scanner scanner = new Scanner(System.in); of sum and store the new total in sum.
System.out.print("Enter a number: ");
int n = scanner.nextInt();
int sum = 0;
Scanner scanner = new Scanner(System.in); Random class is part of the java.util package and provides
System.out.print("Enter a number: "); methods for generating random numbers of various types
int n = scanner.nextInt(); (integers, doubles, booleans, etc.) .
while (num!=n){
num = rand.nextInt(101);
System.out.print(num +","); Output:
num++; Enter a number:
} 660,46,3,47,61,70,12,85,2,13,78,3,16,64,57,97,90,14,81,88,75,
75,95,90,89,67,10,47,9,15,20,30,68,45,19,64,36,85,93,81,29,86
scanner.close(); ,4,4,17,83,48,1,23,7,7,45,68,32,29,12,20,4,51,88,78,52,66,53,1
} 00,72,59,89,
}
Program 19 – do while loop
Enter a password.
The program should ensure the password entered is non-empty and should be at least 6
characters long. If the password does not meet these conditions, the program should repeatedly
prompt the user to re-enter the password until a valid one is entered.
Requirements:
import java.util.Scanner;
public class PasswordEntry {
isEmpty() is used to check if a
public static void main(String[] args) { String is empty. A string is
Scanner scanner = new Scanner(System.in); considered empty if it contains no
String password; characters, i.e., its length is 0.
do {
System.out.print("Enter your password: ");
password = scanner.nextLine(); Output:
Enter your password:
if (password.isEmpty()) {
Password cannot be empty.
System.out.println("Password cannot be empty. Please enter a valid password.");
} Please enter a valid password.
else if (password.length() < 6) { Enter your password:
System.out.println("Password is too short. It must be at least 6 characters long."); nhjPassword is too short. It
} must be at least 6 characters
long.
} while (password.isEmpty() || password.length() < 6); Enter your password:
dfr123Password accepted.
System.out.println("Password accepted.");
scanner.close();
} }
Program 20 – class and object
•lass: public class laptop {
• A class in Java is a blueprint or template for String Name;
creating objects. It defines the properties String Processor;
(attributes/fields) and behaviors (methods) int Ram;
that the objects created from the class will int Price;
have.
• It is a fundamental building block in Java's public static void main(String[] args) {
Object-Oriented Programming (OOP) laptop lap1 = new laptop();
paradigm. lap1.Price = 10000;
• A class can contain fields (variables) and System.out.println(lap1.Price);
methods (functions), and it can also have
constructors, static blocks, inner classes, etc. laptop lap2 = new laptop();
lap2.Price = 8000;
System.out.println(lap2.Price);
Here
Class is a function System.out.println(lap2.Processor);
lap1 & lap2 are object System.out.print(lap1.Ram);
}
}
Program 21 – class, object and function
class movie
{
int ticket = 150;
int parking = 40;
int snacks = 200;
int sum;
void total()
{
sum = ticket + parking + snacks;
System.out.println(sum);
}
public static void main(String[] args)
{
movie expenditure = new movie();
expenditure.total();
}
}
Program 22 – class, object and function
public class Car {
String make;
Create a class called Car that String model;
int year;
represents a car with properties like
public void displayInfo() {
make, model, and year. The class System.out.println("Car Make: " + make);
should have methods to display the System.out.println("Car Model: " + model);
car's information and simulate driving System.out.println("Car Year: " + year);
the car. }
public void drive() {
System.out.println("The " + make + " " + model + " is now driving!");
}
public static void main(String[] args) {
Car myCar = new Car();
Output:
Car Make: Toyota myCar.make = "Toyota";
Car Model: Corolla myCar.model = "Corolla";
Car Year: 2020 myCar.year = 2020;
The Toyota Corolla is now driving!
myCar.displayInfo();
myCar.drive();
} }
Program 23 – Recursion
Fibonacci import java.util.Scanner; Factorial
public class Fibonacci { public class Factorial {
public static void main(String[] args) { public static void main(String[] args) {
int n = 6; Scanner scan = new Scanner(System.in);
System.out.print("Enter a number: ");
// Method to calculate nth Fibonacci number int number = scan.nextInt();
public static int fibonacci(int n) { // Call the factorial method and store the result
// Base Case: F(0) = 0, F(1) = 1 int result = facto(number);
if (n <= 1) { System.out.println("Factorial of " + number + " is " +
return n; result);
} scan.close();
// Recursive Case: F(n) = F(n-1) + F(n-2) }
return fibonacci(n - 1) + fibonacci(n - 2); public static int facto(int number) {
} // Base Case: if n is 0, the factorial is 1
if (number == 0) {
System.out.println("Fibonacci of " + n + " is: " + return 1;
fibonacci(n)); }
} // Recursive Case: n * factorial(n - 1)
} else {
return number * facto(number - 1);
}
}}
Program 24 – Parameter
public class snacks {
Method or function Parameters: These are defined in the void chocolate(int Rs)
function signature and allow data to be passed to the {
function when it is invoked. System.out.println("The cost of chocolate is " + Rs);
}
void juice (int Rs, int Rs1)
{
System.out.println("The cost of juice is "+ Rs + " & " + Rs1);
}
void icecream (String R, int Rs)
{
Output System.out.println("The cost of icecream " + R + " is " + Rs);
The cost of chocolate is 20 }
The cost of juice is 50 & 60 public static void main (String args [])
The cost of icecream vennila is 40 {
The cost of icecream strawberry is 40 snacks cost = new snacks();
cost.chocolate(20);
cost.juice(50,60);
cost.icecream("vennila ", 40);
cost.icecream("strawberry ", 40);
} }
Program 25 – parameter with integer return
public class snacks {
int chocolate (int Rs, int Rs1) {
*In void function the value cannot be return
int c = Rs + Rs1;
return c;
}
int juice (int Rs, int Rs1) {
int c = Rs + Rs1;
return c;
}
void icecream (String R, int Rs)
{
Output System.out.println("The cost of icecream " + R + " is " + Rs);
The cost of icecream vennila is 40 }
The cost of icecream strawberry is 40 public static void main (String args []) {
The cost of chocolate is 99 snacks cost = new snacks();
The cost of juice is 110 int choco = cost.chocolate(20,79);
int jui = cost.juice(50,60);
cost.icecream("vennila ", 40);
cost.icecream("strawberry ", 40);
System.out.println("The cost of chocolate is " + choco);
System.out.println("The cost of juice is " + jui);
} }
Program 26 – Parameter with string return
public class snacks {
int chocolate (int Rs, int Rs1) {
int c = Rs + Rs1;
return c;
}
int juice (int Rs, int Rs1) {
int c = Rs + Rs1;
return c;
}
String icecream (String a)
{
return a;
Output: }
The cost of chocolate is 99 public static void main (String args []) {
The cost of juice is 110 snacks cost = new snacks();
The name of icecream is vennila int choco = cost.chocolate(20,79);
int jui = cost.juice(50,60);
String Ju = cost.icecream("vennila ");
System.out.println("The cost of chocolate is " + choco);
System.out.println("The cost of juice is " + jui);
System.out.println("The name of icecream is " + Ju);
} }
Program 27 - Parameter with string return
}
Output: public static void main(String [] args)
Enter the Number 29 {
The number is odd number Scanner scan = new
Enter the Number 30 Scanner(System.in);
The number is even number System.out.print("Enter the Number
");
int a =scan.nextInt();
number type = new number();
Program 28 – function overloading
Single Inheritance means One class inherits from another class class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
Output: @Override
Dog barks void sound() {
System.out.println("Dog barks");
}
}
Program 34 – inheritance & override
class vehicle {
String brand;
int year;
void startEngine () {
System.out.println("Engine Starts");
} }
class car extends vehicle {
String fuelType;
@Override
void startEngine() {
System.out.println("Car Engine Starts");
} Output
void drive()
{ Truck engine starts
System.out.println("Car is driving"); Truck is hauling
} }
class truck extends vehicle{ Car Engine Starts
int loadcapacity; Car is driving
@Override
void startEngine() {
System.out.println("Truck engine starts");
}
void haul(){
System.out.println("Truck is hauling");
} }
public class inheritance {
public static void main(String[] args) {
truck V1 = new truck();
V1.startEngine();
V1.haul();
car V2 = new car();
V2.startEngine();
V2.drive();
Program 35 – Super keyword
The super keyword in Java is used to refer to the public class Main {
immediate parent class and can be used to call the public static void main(String[] args) {
Vijay vijay = new Vijay();
parent class's constructor, methods, or fields. It helps
vijay.act();
in accessing or invoking members of the superclass }
from the subclass. }
class Actor {
Actor() {
Parent class’s constructor and methos accessing System.out.println("An actor likes to become a Chief Minister");
}
void act() {
System.out.println("Actor role is acting");
}
Output: }
An actor likes to become a Chief Minister class Vijay extends Actor {
Vijay likes to enter a politics Vijay() {
Actor role is acting super();
Vijay need to act in a movie. System.out.println("Vijay likes to enter a politics");
}
void act() {
super.act();
System.out.println("Vijay need to act in a movie.");
}
}
Program 36 – super keyword