Java Pretical
Java Pretical
import java.util.Scanner;
// Print results
System.out.println("Reverse of " + originalNum + " is " +
reverse);
System.out.println("Sum of digits of " + originalNum + "
is " + sum);
// Close scanner
scanner.close();
}
}
2. Write a program to accept array of 10 no. and remove the
occurrence of the given no from the array in java
programming.
import java.util.Scanner;
if (sum == originalNum) {
System.out.println(originalNum + " is an Armstrong
number.");
} else {
System.out.println(originalNum + " is not an
Armstrong number.");
}
}
}
4.Write a program to create an applet to accept a string in a
textbox and change all the letters in d given string to
Uppercase at the click of 'show' button in java programming.
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
/*
<applet code="UppercaseApplet" width=400 height=200>
</applet>
*/
// Add components
add(new Label("Enter String:"));
add(inputField);
add(showButton);
// Register event
showButton.addActionListener(this);
}
import java.util.Scanner;
// Display result
System.out.println("Factorial of " + num + " is " +
factorial);
}
scanner.close();
}
}
6. Write a program to print the sum of all the odd number
between 1-100.
import java.util.Scanner;
import java.util.Scanner;
// User-defined exception
class NumberIsZeroException extends Exception {
public NumberIsZeroException(String message) {
super(message);
}
}
try {
String input = scanner.nextLine();
// Display result
if (original == reverse) {
System.out.println(original + " is a Palindrome.");
} else {
System.out.println(original + " is not a Palindrome.");
}
} catch (NumberIsZeroException e) {
System.out.println(e.getMessage());
} catch (NumberFormatException e) {
System.out.println("Number is Invalid");
}
scanner.close();
}
}
9.Write a Java program Create abstract class shape. Drive
three classes sphere. Cone and cylinder from it.
Calculate Area and volume of all(use method overriding)
import java.util.Scanner;
// Sphere class
class Sphere extends Shape {
double radius;
Sphere(double radius) {
this.radius = radius;
}
@Override
void calculateArea() {
double area = 4 * Math.PI * radius * radius;
System.out.println("Sphere Surface Area: " + area);
}
@Override
void calculateVolume() {
double volume = (4.0 / 3.0) * Math.PI * radius * radius
* radius;
System.out.println("Sphere Volume: " + volume);
}
}
// Cone class
class Cone extends Shape {
double radius, height;
@Override
void calculateArea() {
double slantHeight = Math.sqrt(radius * radius +
height * height);
double area = Math.PI * radius * (radius +
slantHeight);
System.out.println("Cone Surface Area: " + area);
}
@Override
void calculateVolume() {
double volume = (1.0 / 3.0) * Math.PI * radius * radius
* height;
System.out.println("Cone Volume: " + volume);
}
}
// Cylinder class
class Cylinder extends Shape {
double radius, height;
@Override
void calculateArea() {
double area = 2 * Math.PI * radius * (radius + height);
System.out.println("Cylinder Surface Area: " + area);
}
@Override
void calculateVolume() {
double volume = Math.PI * radius * radius * height;
System.out.println("Cylinder Volume: " + volume);
}
}
// Main class
public class ShapeTest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Sphere
System.out.print("Enter radius of Sphere: ");
double r1 = scanner.nextDouble();
Shape sphere = new Sphere(r1);
sphere.calculateArea();
sphere.calculateVolume();
// Cone
System.out.print("\nEnter radius and height of Cone:
");
double r2 = scanner.nextDouble();
double h2 = scanner.nextDouble();
Shape cone = new Cone(r2, h2);
cone.calculateArea();
cone.calculateVolume();
// Cylinder
System.out.print("\nEnter radius and height of
Cylinder: ");
double r3 = scanner.nextDouble();
double h3 = scanner.nextDouble();
Shape cylinder = new Cylinder(r3, h3);
cylinder.calculateArea();
cylinder.calculateVolume();
scanner.close();
}
}
// Convert to Fahrenheit
double fahrenheit = (celsius * 9 / 5) + 32;
// Display result
System.out.println("Temperature in Fahrenheit: " +
fahrenheit);
scanner.close();
}
}
import java.util.Scanner;
// Interface Shape
interface Shape {
double getArea();
}
// Rectangle class
class Rectangle implements Shape {
double length, width;
// Circle class
class Circle implements Shape {
double radius;
Circle(double radius) {
this.radius = radius;
}
// Triangle class
class Triangle implements Shape {
double base, height;
// Main class
public class ShapeInterfaceDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Rectangle
System.out.print("Enter length and width of Rectangle:
");
double length = scanner.nextDouble();
double width = scanner.nextDouble();
Shape rectangle = new Rectangle(length, width);
System.out.println("Area of Rectangle: " +
rectangle.getArea());
// Circle
System.out.print("\nEnter radius of Circle: ");
double radius = scanner.nextDouble();
Shape circle = new Circle(radius);
System.out.println("Area of Circle: " + circle.getArea());
// Triangle
System.out.print("\nEnter base and height of Triangle:
");
double base = scanner.nextDouble();
double height = scanner.nextDouble();
Shape triangle = new Triangle(base, height);
System.out.println("Area of Triangle: " +
triangle.getArea());
scanner.close();
}
}