[Go to site: main page, start]

0% found this document useful (0 votes)
3 views15 pages

Java Programs Updated

The document contains multiple Java programs demonstrating various programming concepts including weight conversion, complex number arithmetic, area calculation using method overloading, palindrome checking, string manipulation, list operations, exception handling, matrix multiplication, inheritance, interfaces, package creation, multithreading, custom exceptions, and file processing. Each program is self-contained and showcases specific functionalities or concepts in Java. The examples serve as practical implementations for learning Java programming.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views15 pages

Java Programs Updated

The document contains multiple Java programs demonstrating various programming concepts including weight conversion, complex number arithmetic, area calculation using method overloading, palindrome checking, string manipulation, list operations, exception handling, matrix multiplication, inheritance, interfaces, package creation, multithreading, custom exceptions, and file processing. Each program is self-contained and showcases specific functionalities or concepts in Java. The examples serve as practical implementations for learning Java programming.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

[Link] implement weight conversion program (Kg to lbs).

import [Link];
public class WeightConverter {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter weight in kilograms: ");
double kg = [Link]();
[Link]("Weight in pounds: " + (kg * 2.20462));
[Link]();
}
}
[Link] perform addition and subtraction of complex numbers using
class and objects.
class ComplexCalc {
static void add(double r1, double i1, double r2, double i2) {
[Link]("Sum: " + (r1 + r2) + " + " + (i1 + i2) + "i");
}
static void sub(double r1, double i1, double r2, double i2) {
[Link]("Diff: " + (r1 - r2) + " + " + (i1 - i2) + "i");
}
public static void main(String[] args) {
add(10.5, 4.0, 5.5, 2.0);
sub(10.5, 4.0, 5.5, 2.0);
}
}
[Link] perform area calculation using method overloading.
public class Area {
void calc(int side) {
[Link]("Square: " + (side * side));
}
void calc(int l, int b) {
[Link]("Rectangle: " + (l * b));
}
public static void main(String[] args) {
Area a = new Area();
[Link](5);
[Link](4, 6);
}
}
[Link] check whether the given string is palindrome or not using
command line arguments.
import [Link];
public class Palindrome {
public static void main(String[] args) {
[Link]("Enter a word: ");
String str = new Scanner([Link]).next(), rev = "";
for (int i = [Link]() - 1; i >= 0; i--) rev += [Link](i);
[Link](str + ([Link](rev) ? " is " : " is not ") + "a
palindrome.");
}
}
[Link] perform string manipulation.
public class StringManipulation {
public static void main(String[] args) {
String s = "Hello World";
[Link]("Uppercase: " + [Link]());
[Link]("Lowercase: " + [Link]());
[Link]("Substring: " + [Link](0, 5));
[Link]("Length: " + [Link]());
}
}
[Link] a program to fill names into a list. Also, copy them in
reverse order into another list.
import [Link].*;
public class ListDemo {
public static void main(String[] args) {
List<String> original = new ArrayList<>([Link]("Alice",
"Bob", "Charlie"));
[Link]("Original List: " + original);
[Link](original);
[Link]("Reversed List: " + original);
}
}
[Link] to demonstrate the use of any two built-in exceptions in
java.
public class ExceptionExample {
public static void main(String[] args) {
try {
int a = 5 / 0;
} catch (ArithmeticException e) {
[Link]("Math Error: " + [Link]());
}

try {
int[] arr = new int[2];
arr[5] = 10;
} catch (ArrayIndexOutOfBoundsException e) {
[Link]("Array Error: " + [Link]());
}
}
}
[Link] perform multiplication of matrices using class and objects.
import [Link];
public class Matrix {
int[][] m = new int[2][2];
Matrix(Scanner sc) {
for (int i = 0; i < 4; i++) m[i / 2][i % 2] = [Link]();
}
void mul(Matrix o) {
for (int i = 0; i < 2; i++)
[Link]("[" + (m[i][0] * o.m[0][0] + m[i][1] * o.m[1][0])
+ ", " + (m[i][0] * o.m[0][1] + m[i][1] * o.m[1][1]) + "]");
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
Matrix m1 = new Matrix(sc), m2 = new Matrix(sc);
[Link](m2);
}
}
[Link] multilevel inheritance process employee salary calculation.
class Person {
String name = "John";
}
class Employee extends Person {
double salary = 50000;
}
public class Manager extends Employee {
double bonus = 10000;
void show() {
[Link]("Name:" + name + "\n Total Salary: " + (salary +
bonus));
}
public static void main(String[] args) {
Manager m = new Manager();
[Link]();
}
}
[Link] multiple inheritance for student report card.
interface Marks {
void showMarks();
}
interface Attendance {
void showAttendance();
}
public class Student implements Marks, Attendance {
public void showMarks() {
[Link]("Marks: 85");
}
public void showAttendance() {
[Link]("Attendance: 90%");
}
public static void main(String[] args) {
Student s = new Student();
[Link]();
[Link]();
}

}
[Link] interface for area calculation for different shapes.
interface Shape {
double area();
}
class Circle implements Shape {
public double area() {
return 3.14 * 5 * 5;
}
}
class Rectangle implements Shape {
public double area() {
return 4 * 5;
}
}
public class ShapeMain {
public static void main(String[] args) {
Shape c = new Circle(), r = new Rectangle();
[Link]("Circle Area: " + [Link]());
[Link]("Rectangle Area: " + [Link]());

}
[Link] a package called Arithmetic that contains methods to
deal with all arithmetic operators. Also write a program to use the
package.
// File: mypack/[Link]
package mypack;
public class Arithmetic {
public int add(int a, int b) { return a + b; }
public int sub(int a, int b) { return a - b; }
public int mul(int a, int b) { return a * b; }
public int div(int a, int b) { return a / b; }
}

// File: [Link]
import [Link];
public class Main {
public static void main(String[] args) {
Arithmetic a = new Arithmetic();
[Link]("Add: " + [Link](5, 3) + "\nSubtract: " + [Link](5,
3));
}
}
[Link] a java program to implement multi-threading.
public class MyThread extends Thread {
public void run() {
for (int i = 1; i <= 5; i++) [Link](i);
}
public static void main(String[] args) {
MyThread t = new MyThread();
[Link]();
}
}
[Link] and exception called: Marks Out of bound: Exception,
that is thrown if the entered test marks are greater than 30.
class MarksOutOfBoundsException extends Exception {
MarksOutOfBoundsException(String s) {
super(s);
}
}
public class BoundsMain {
public static void main(String[] args) {
int marks = 35;
try {
if (marks > 30)
throw new MarksOutOfBoundsException("Marks out of bounds");
} catch (MarksOutOfBoundsException e) {
[Link]("Exception: " + [Link]());
}
}
}
[Link] Processing using Byte stream.
import [Link];
import [Link];
import [Link];
public class FileCopy {
public static void main(String[] args) throws IOException {
FileInputStream in = new FileInputStream("[Link]");
FileOutputStream out = new FileOutputStream("[Link]");
int c;
while ((c = [Link]()) != -1) [Link](c);
[Link]();
[Link]();
[Link]("File copied successfully.");
}
}

You might also like