JAVA Codes
JAVA Codes
JAVA Codes
in);
import java.util.Scanner; System.out.print("Enter the starting number (m): ");
int m = scanner.nextInt();
public class PrimeNumbers {
public static void main(String[] args) { System.out.print("Enter the ending number (n): ");
Scanner scanner = new Scanner(System.in); int n = scanner.nextInt();
System.out.print("Enter the starting number (m): "); for (int i = m; i <= n; i++) {
int m = scanner.nextInt(); int num = i;
int sum = 0;
System.out.print("Enter the ending number (n): "); int digits = String.valueOf(i).length();
int n = scanner.nextInt();
while (num != 0) {
for (int i = m; i <= n; i++) { int remainder = num % 10;
if (i <= 1) continue; sum += Math.pow(remainder, digits);
boolean isPrime = true; num /= 10;
}
for (int j = 2; j <= Math.sqrt(i); j++) {
if (i % j == 0) { if (sum == i) {
isPrime = false; System.out.println(i);
break; }
} }
}
scanner.close();
if (isPrime) { }
System.out.println(i); }
}
}
Calculate the Volume of a Sphere
import java.util.*;
scanner.close();
} import java.math.*;
}
class Main
PERFECT NUMBERS.
{
import java.util.Scanner;
public static void main(String args[])
public class PerfectNumbers { {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); Scanner abc=new Scanner(System.in);
float r=abc.nextFloat();
System.out.print("Enter the starting number (m): ");
int m = scanner.nextInt(); double v=(4f/3f)* Math.PI*Math.pow(r,3);
System.out.print(String.format("%.2f",v));
System.out.print("Enter the ending number (n): ");
int n = scanner.nextInt(); }
int[] girlsAges = new int[N]; b) write a java program to implement access specifiers with the
int[] boysAges = new int[M]; help of packages.
System.out.println("Enter the ages of girls:"); Step 1: Create the com.company package with a class Employee.
for (int i = 0; i < N; i++) {
girlsAges[i] = scanner.nextInt(); package com.company;
}
public class Employee {
System.out.println("Enter the ages of boys:"); public int employeeId;
for (int i = 0; i < M; i++) { public String employeeName;
boysAges[i] = scanner.nextInt();
} private double salary;
person.displayDetails(); System.out.print(a+b);
}
System.out.println("Name: " + person.getName());
System.out.println("Age: " + person.getAge()); }
}
} Time taken for one harmonic motion
import java.util.*;
program that calculates the displacement (s) import java.math.*;
import java.util.*; class Main
class Main {
{ public static void main(String args[])
public static void main(String args[]) {
{ Scanner atz=new Scanner(System.in);
float u,a,t; double g=9.8;
Scanner atz=new Scanner(System.in); double L=atz.nextDouble();
u=atz.nextFloat(); double t=2 * Math.PI * Math.sqrt(L/g);
a=atz.nextFloat(); System.out.printf("%.2f",t);
t=atz.nextFloat(); }
float s=(u*t)+(0.5f *a*(t*t)); }
System.out.print(s);
electricity bill (taking input from the keyboard)
}
import java.util.*;
}
class Main
Simple Interest {
public static void main(String args[]) System.out.print("Hot Weather");
{ }
Scanner sc=new Scanner(System.in); else if(n>=40)
double b=0d; {
int id=sc.nextInt(); System.out.print("Very hot Weather");
int u=sc.nextInt(); }
if(u<=199)
b=u*1.20; else{
else if(u>=200 && u<=399) System.out.print("Invalid input");
b=u*1.50; }
else if(u>=400 && u<=599) scn.close();
b=u*1.80; }
else }
b=u*2.00; Arithmetic Progression(taking input from the
if(b>400) keyboard)
b+=(0.15*b); import java.util.*;
if(b<100) class Main
b=100; {
System.out.print(b); public static void main(String args[])
} {
} Scanner scn=new Scanner(System.in);
case 3: }
break; {
System.out.print("Invalid Choice"); {
} int i,n=s.nextInt();
} for(i=0;i<n;i++)
} {
arr[i][0]=s.next();
Weekly Temperature Analysis (storing arrays)
arr[i][1]=s.next();
import java.util.*;
}
class Main
char[] grades={'A','B','C','D','E'};
{
for(char grade:grades)
public static void main(String args[])
{
{
System.out.println("");
int n=7,i;
System.out.print(grade+":");
double[] t=new double[7];
for(i=0;i<n;i++)
double sum=0.00;
{
Scanner s=new Scanner(System.in);
boolean first=true;
for(i=0;i<n;i++)
if(arr[i][1].charAt(0)==grade)
{
{
t[i]=s.nextDouble();
if(!first)
sum+=t[i];
System.out.print(" ");
}
System.out.print(" "+arr[i][0]);
double max=t[0];
first=false; }
} }
} Employee details (Creating Class & Creating
} ,Implementing Method)
} import java.util.*;
}
if(rn[i]==rollno) System.out.print("Stock
Percentage:"+stockpercentage+"%");
{
}
System.out.println("Roll Number: "+rn[i]+", Name:
"+name[i]+", Marks: "+marks[i]); catch(ArithmeticException e){
found=true; System.out.print(e.getMessage());
break; }
} }
} }
} user2.start();
} user2.join();
user3.start();
return bal; }
} catch (InterruptedException e) {
} e.printStackTrace();
}
{ import java.io.IOException;
outputStream.write(byteData);
public class Main { }
public static void main(String[] args) { System.out.println("File Copied Successfully");
String filePath = "input.txt"; }
int lineCount = 0; catch (IOException e) {
int wordCount = 0; System.out.println("Error: " + e.getMessage());
int charCount = 0; }
}
try (BufferedReader reader = new }
BufferedReader(new FileReader(filePath))) {
reads a file and displays the file on the screen
String line; (BufferedReader)
while ((line = reader.readLine()) != null) { import java.io.BufferedReader;
lineCount++; import java.io.FileReader;
charCount += line.replaceAll("\\s", "").length(); import java.io.IOException;
String[] words = line.trim().split("\\s+");
wordCount += words.length; public class Main {
} public static void main(String[] args) {
System.out.println("Lines: " + lineCount); String filePath = "input.txt";
System.out.println("Words: " + wordCount); try (BufferedReader reader = new
System.out.println("Characters: " + charCount); BufferedReader(new FileReader(filePath))) {
} String line;
catch (IOException e) { int lineNumber = 1;
System.out.println("Error reading file: " + while ((line = reader.readLine()) != null) {
e.getMessage()); System.out.println(lineNumber + ": " + line);
} lineNumber++;
} }
} }
Copying File Contents (FileInputStream & catch (IOException e) {
FileOutputStream)
System.out.println("Error reading file: " +
import java.io.FileInputStream; e.getMessage());
import java.io.FileOutputStream; }
import java.io.IOException; }
}
public class Main { Calculator Using Swings
public static void main(String[] args) { import javax.swing.*;
String sourceFile = "source.txt"; import java.awt.*;
String destinationFile = "destination.txt"; class Main {
public static void main(String[] args) {
try (FileInputStream inputStream = new JFrame frame = new JFrame("Calculator");
FileInputStream(sourceFile);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CL
FileOutputStream outputStream = new OSE);
FileOutputStream(destinationFile)) {
frame.setSize(300, 400);
int byteData;
JPanel panel = new JPanel();
while ((byteData = inputStream.read()) != -1) {
panel.setLayout(new GridLayout(4, 4, 5, 5)); genderGroup.add(femaleButton);
String[] buttons = {
"7", "8", "9", "*", gbc.gridx = 0; gbc.gridy = 0; formPanel.add(new
JLabel("Name:"), gbc);
"4", "5", "6", "/",
gbc.gridx = 1; formPanel.add(nameField, gbc);
"1", "2", "3", "+",
gbc.gridx = 0; gbc.gridy = 1; formPanel.add(new
"0", ".", "=", "-" JLabel("Address:"), gbc);
}; gbc.gridx = 1; formPanel.add(addressField, gbc);
for (String text : buttons) { gbc.gridx = 0; gbc.gridy = 2; formPanel.add(new
JButton button = new JButton(text); JLabel("Gender:"), gbc);
panel.add(button); gbc.gridx = 1; formPanel.add(new JPanel(new
FlowLayout(FlowLayout.LEFT)) {{
}
add(maleButton); add(femaleButton);
frame.add(panel);
}}, gbc);
frame.setVisible(true);
}
add(formPanel, BorderLayout.CENTER);
}
Student details using swings
JPanel buttonPanel = new JPanel();
import javax.swing.*;
JButton saveButton = new JButton("Save"),
import java.awt.*; cancelButton = new JButton("Cancel");
buttonPanel.add(saveButton);
public class Main extends JFrame { buttonPanel.add(cancelButton);
public Main() { add(buttonPanel, BorderLayout.SOUTH);
setTitle("Student Detail");
setSize(300, 200); saveButton.addActionListener(e ->
setDefaultCloseOperation(EXIT_ON_CLOSE); JOptionPane.showMessageDialog(null,
import java.util.*; {
Book b1=new Book();
} {
private int eid;
} }
for(i=0;i<r;i++) }
{
for(j=0;j<c;j++) public String getName() {
{ return name;
arr[i][j]=s.nextInt(); }
} return salary;
}
{ return 0.0;
for(j=0;j<c;j++) }
} }
} }
public Programmer(String name, String address, double
salary, String language) {
class Manager extends Employee {
super(name, address, salary, language);
public Manager(String name, String address, double
salary) { }
super(name, address, salary, "Manager");
} @Override
@Override public double calculateBonus() {
public double calculateBonus() { return getSalary() * 0.12;
return getSalary() * 0.15; }
} @Override
@Override public String generatePerformanceReport() {
public String generatePerformanceReport() { return "Performance report for Programmer " +
getName() + ": Excellent";
return "Performance report for Manager " +
getName() + ": Excellent"; }
} public void debugCode() {
public void manageProject() { System.out.println("Programmer " + getName() + "
is debugging code in Python");
System.out.println("Manager " + getName() + " is
managing a project."); }
} }
}
public class Main {
class Developer extends Employee { public static void main(String[] args) {
private String language; Manager m = new Manager("Raju Dev", "1 ABC St",
80000);
public Developer(String name, String address, double
salary, String language) { Developer d = new Developer("Neeraj Gupta", "2
PQR St", 72000, "Java");
super(name, address, salary, "Developer");
this.language = language; Programmer p = new Programmer("Sujay Raj", "3
ABC St", 76000, "Python");
}
System.out.println("Manager's Bonus: $" +
m.calculateBonus());
@Override public double calculateBonus() { System.out.println("Developer's Bonus: $" +
return getSalary() * 0.10; d.calculateBonus());
} System.out.println("Programmer's Bonus: $" +
p.calculateBonus());
@Override
public String generatePerformanceReport() { System.out.println(m.generatePerformanceReport());
return "Performance report for Developer " + System.out.println(d.generatePerformanceReport());
getName() + ": Good";
System.out.println(p.generatePerformanceReport());
}
m.manageProject();
public void writeCode() {
d.writeCode();
System.out.println("Developer " + getName() + " is
writing code in " + language); p.debugCode();
} }
} }
Parent share to sons and daughter (Inheritance,
class Programmer extends Developer { Encapsulation, Constructor Chaining,
Polymorphism, Aggregation, Arithmetic
Operations, Modularity, Abstraction, Code public Son1(double amt) {
Reusability, Overriding, Scanner, Type Casting, super(amt);
Input Handling, Resource Management, Output }
Formatting, Design Principles.)
}
import java.util.Scanner;
class Son2 extends FM {
public Son2(double amt) {
class FM {
super(amt);
double amt;
}
public FM(double amt) {
this.amt = amt;
public void giveToSister(Daughter daughter) {
}
double toDaughter = this.amt * 0.10;
daughter.addAmount(toDaughter);
public void addAmount(double add) {
this.deductAmount(toDaughter);
this.amt += add;
}
}
}
System.out.println(savingsAccount.accountDetails()); }
System.out.println(); @Override