Basic Arithmetic Operations
Basic Arithmetic Operations
import java.util.Scanner;
2. Multiplication Table
import java.util.Scanner;
System.out.println("Enter a number:");
if (number <= 1) {
isPrime = false;
} else {
if (number % i == 0) {
isPrime = false;
break;
System.out.println(number + " is " + (isPrime ? "a Prime number." : "not a Prime number."));
}
}
import java.util.Scanner;
int n = scanner.nextInt();
if (isPerfect(i)) {
System.out.println(i);
int sum = 0;
if (num % i == 0) {
sum += i;
import java.util.Scanner;
System.out.println("Enter a number:");
int sum = 0;
while (number != 0) {
number /= 10;
class AreaCalculator {
class Person {
int pid;
String pname;
int age;
String gender;
// Default constructor
public Person() {
this.pid = 0;
this.pname = "";
this.age = 0;
this.gender = "";
// Parameterized constructor
this.pid = pid;
this.pname = pname;
this.age = age;
this.gender = gender;
System.out.println("ID: " + pid + ", Name: " + pname + ", Age: " + age + ", Gender: " + gender);
persons[i].display();
import java.util.Scanner;
class Product {
int pid;
String pname;
double price;
pid = scanner.nextInt();
pname = scanner.next();
price = scanner.nextDouble();
System.out.println("ID: " + pid + ", Name: " + pname + ", Price: " + price);
products[i].accept();
product.display();
import java.util.Scanner;
class Employee {
String name;
this.name = name;
return name;
int n = scanner.nextInt();
// Sorting
// Swap
employees[j + 1] = temp;
System.out.println(emp.getName());
import java.util.Scanner;
class CricketPlayer {
int pid;
String pname;
this.pid = pid;
this.pname = pname;
this.totalRuns = totalRuns;
this.inningsPlayed = inningsPlayed;
double average() {
void display() {
int n = scanner.nextInt();
maxAvgPlayer.display();
import java.util.Scanner;
class Stud {
int sid;
this.sid = sid;
this.sname = sname;
this.sclass = sclass;
void display() {
System.out.println("ID: " + sid + ", Name: " + sname + ", Class: " + sclass);
int n = scanner.nextInt();
this.length = length;
this.width = width;
double area() {
this.base = base;
this.height = height;
double area() {
}
}
1. Maximum Class:
package Mathematics;
2. Power Class:
package Mathematics;
class Circle {
double radius;
Circle(double radius) {
this.radius = radius;
}
double area() {
return Math.PI * radius * radius;
}
}
16. Interface Shape with area() method for Circle and Sphere:
interface Shape {
double area();
}
@Override
public double area() {
return Math.PI * radius * radius;
}
}
final class Sphere implements Shape {
private final double radius;
@Override
public double area() {
return 4 * Math.PI * radius * radius;
}
}
interface IntegerCheck {
void check(int num);
}
18. Abstract class Shape with calc_area() & calc_volume() for Sphere and Cone:
@Override
double calc_area() {
return 4 * Math.PI * radius * radius;
}
@Override
double calc_volume() {
return (4.0 / 3) * Math.PI * radius * radius * radius;
}
}
@Override
double calc_area() {
return Math.PI * radius * (radius + Math.sqrt(height * height + radius * radius));
}
@Override
double calc_volume() {
return (1.0 / 3) * Math.PI * radius * radius * height;
}
}
class Indoor {
public Indoor() {
this.players = players;
System.out.println(player);
class Outdoor {
public Outdoor() {
this.players = players;
System.out.println(player);
indoor.display();
outdoor.display();
import java.util.Scanner;
try {
System.out.println("Eligible to vote");
} catch (VotingAgeException e) {
System.out.println(e.getMessage());
}
21. Check palindrome and handle exceptions
import java.util.Scanner;
try {
} catch (NumberFormatException e) {
System.out.println("Invalid number");
} catch (ZeroException e) {
System.out.println(e.getMessage());
try {
int sum = 0;
for (char digit : String.valueOf(num).toCharArray()) sum += digit - '0';
} catch (OutOfRangeException e) {
System.out.println(e.getMessage());
import java.io.*;
int ch;
writer.write(Character.isDigit(c) ? '*' :
Character.isUpperCase(c) ? Character.toLowerCase(c) :
Character.toUpperCase(c));
reader.close(); writer.close();
import java.io.File;
file.delete();
import java.applet.Applet;
import java.awt.Graphics;
setSize(w, h);
import java.awt.*;
import java.awt.event.*;
import java.util.Scanner;
char op = sc.next().charAt(0);
sc.close();
import java.applet.Applet;
import java.awt.Graphics;
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public InterestCalculatorCompact() {
setTitle("Interest Calculator");
if (e.getSource() == btnCalculate) {
try {
int y = Integer.parseInt(txtYears.getText());
txtTotalAmount.setText(String.valueOf(total));
txtTotalAmount.setText(""); txtInterestAmount.setText("");
new InterestCalculatorCompact();