JAVAnewfile
JAVAnewfile
SEMESTER – IV
Submitted to : Submitted by :
Aim: Design a java program to find the biggest of three given integers.
Code:
import java.util.*;
class BiggestNumberFinder {
public static int findBiggest(int num1, int num2, int num3) {
int max = num1;
return max;
}
}
in.close();
}
}
Output:
Program – 2
Aim: Design a java program to define a class, describe its constructor, over-load the
constructor and instantiate the object.
Code:
class Shapes
{
double len;
double bre;
double result;
Shapes()
{
len=0.0;
bre=0.0;
}
Shapes(double l)
{
len=l;
bre=0.0;
}
Shapes(double l, double b)
{
len=l;
bre=b;
}
void area()
{
if(len!=0.0 && bre==0.0)
{
result=len*len;
System.out.println("AREA SQUARE " + result);
}
if(len!=0.0 && bre!=0.0)
{
result=len*bre;
System.out.println("AREA RECTANGLE " + result);
}
else if(len==0.0 && bre==0.0)
{
System.out.println("NO OUTPUT");
}
}
}
class MyELClass
{
public static void main(String a[])
{
double para1, para2;
para1=10.0;
para2=20.0;
Shapes s1=new Shapes();
Shapes s2=new Shapes(para1);
Shapes s3=new Shapes(para1,para2);
s2.area();
s3.area();
s1.area();
System.out.println("Gurkirat singh, 13313203122" );
}
}
Output:
Program – 3
Code:
class Outer
{
private int outer_x = 100;
void test()
{
Inner inner = new Inner();
inner.display();
inner.hello();
}
class Inner
{
private void hello()
{
System.out.println("Private Method Hello() of Inner Class");
void display()
{
System.out.println("Display: outer_x = " + outer_x);
Outer o=new Outer();
System.out.println("Display: outer_x = " + o.outer_x);
}
}
}
class MyELClass
{
public static void main(String args[])
{
Outer outer = new Outer();
outer.test();
System.out.println("Gurkirat singh, 13313203122" );
}
}
Output:
Program – 4
Aim: Design a java program to print all the real solutions of the quadratic equation
ax^2+bx+c=0, read in the values of a, b, c and use the quadratic formula. If the discriminant
b^2-4ac is negative, display a message stating that there are no real solutions.
Code:
import java.util.*;
class QuadraticEquationSolver {
public static void solveQuadraticEquation(double a, double b, double c) {
double discriminant = b * b - 4 * a * c;
if (discriminant < 0) {
System.out.println("No real solutions exist.");
} else {
double root1 = (-b + Math.sqrt(discriminant)) / (2 * a);
double root2 = (-b - Math.sqrt(discriminant)) / (2 * a);
QuadraticEquationSolver.solveQuadraticEquation(a, b, c);
scanner.close();
System.out.println("Gurkirat singh, 13313203122" );
}
}
Output:
Program – 5
Aim: Design a java program that uses both recursive and non-recursive methods to print the
nth value of the Fibonacci series sequence.
Code:
import java.util.*;
class FibonacciCalculator {
// Non-recursive method to find nth Fibonacci number
public static long fibonacciNonRecursive(int n) {
long a = 0;
long b = 1;
long fib = 0;
if (n == 0) return a;
if (n == 1) return b;
return fib;
}
scanner.close();
System.out.println("Gurkirat singh, 13313203122" );
}
}
Output:
Program – 6
Aim: Design a java program to prompt user for an integer and print all the prime numbers
up to that integer.
Code:
import java.util.*;
class Prime {
int value, num;
int i, flag;
void inputNumber() {
Scanner sc = new Scanner(System.in);
System.out.println("ENTER A NUMBER ");
value = sc.nextInt();
}
void displayPrime() {
for (num = 2; num <= value; num++) {
flag = 0;
for (i = 2; i <= num - 1; i++) {
if (num % i == 0) {
flag = 1;
break;
}
}
if (flag == 0) {
System.out.println(num);
}
}
}
}
class MyELClass {
public static void main(String a[]) {
Prime o = new Prime();
o.inputNumber();
o.displayPrime();
System.out.println("Gurkirat singh, 13313203122" );
}
}
Output:
Program – 7
Code:
import java.util.*;
class P_Check {
void check(String data) {
String rdata = "";
int i, len;
len = data.length();
for (i = len - 1; i >= 0; i--)
rdata = rdata + data.charAt(i);
if (data.equals(rdata))
System.out.println("Entered string is a palindrome.");
else
System.out.println("Entered string is not a palindrome.");
}
}
class MyELClass {
public static void main(String a[]) {
String name;
P_Check pc = new P_Check();
Scanner in = new Scanner(System.in);
System.out.println("Enter a string");
name = in.nextLine();
pc.check(name);
System.out.println("Gurkirat singh, 13313203122" );
}
}
Program – 8
Aim: Design a java program that reads a line of integers, then display each integer and the
sum of all integers.
Code:
import java.util.*;
class IntData {
int arr[];
int i, sum;
IntData(int size) {
arr = new int[size];
sum = 0;
}
void input() {
Scanner sc = new Scanner(System.in);
for (i = 0; i < arr.length; i++) {
arr[i] = sc.nextInt();
}
}
void display() {
for (i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
void sumCal() {
for (i = 0; i < arr.length; i++) {
sum = sum + arr[i];
}
System.out.println("SUM IS " + sum);
}
}
class MyELClass {
public static void main(String a[]) {
IntData obj = new IntData(5);
obj.input();
obj.display();
obj.sumCal();
System.out.println("Gurkirat singh, 13313203122" );
}
}
Output:
Program – 9
Code:
class Stack {
int stck[];
int tos;
Stack(int size) {
stck = new int[size];
tos = -1;
}
int pop() {
if (tos < 0) {
System.out.println("Stack underflow.");
return 0;
} else
return stck[tos--];
}
}
class MyELClass {
public static void main(String args[]) {
Stack mystack1 = new Stack(5);
Stack mystack2 = new Stack(8);
System.out.println("Stack in mystack1:");
for (int i = 0; i < 5; i++)
System.out.println(mystack1.pop());
System.out.println("Stack in mystack2:");
for (int i = 0; i < 8; i++)
System.out.println(mystack2.pop());
System.out.println(("Gurkirat singh, 13313203122" );
}
}
Output:
Program – 10
Aim: Design a java program to create an abstract class named Shape, that contains an empty
method called numberOfSides(). Provide four classes named Trapezoid, Triangle, Rectangle
and Hexagon such that each one of the classes contains only the method numberOfSides(),
that contains number of sides in each geometrical figure.
Code:
Output:
Program – 11
Code:
class MyArea {
void area() {
System.out.println("A concrete function called Area()");
}
}
class MyELClass {
public static void main(String a[]) {
MyArea ma; // Base class object
ma = new SQArea(); // Referring to SQArea
ma.area(); // Calling SQArea method (Dynamic Polymorphism)
ma = new RECTArea(); // Referring to RECTArea
ma.area(); // Calling RECTArea method (Dynamic Polymorphism)
System.out.println("Gurkirat singh, 13313203122" );
}
}
Output:
Program – 12
Code:
interface IntStack {
void push(int item); // Store an item.
int pop(); // Retrieve an item.
}
class ELClass1 {
public static void main(String args[]) {
DynStack mystack1 = new DynStack(5);
DynStack mystack2 = new DynStack(8);
System.out.println("Stack in mystack1:");
for (int i = 0; i < 12; i++)
System.out.println(mystack1.pop());
System.out.println("Stack in mystack2:");
for (int i = 0; i < 20; i++)
System.out.println(mystack2.pop());
System.out.println("Gurkirat singh, 13313203122" );
}
}
Output:
Program – 13
Aim: Design a java program to create a customized exception and also make use of all 5
exception keywords.
Code:
class Employee {
public void decideSal(String s1) throws NSalException, PSalException,
NumberFormatException {
int sal = Integer.parseInt(s1);
if (sal <= 0) {
NSalException no = new NSalException("Invalid Salary");
throw(no);
} else {
PSalException po = new PSalException("Valid Salary");
throw(po);
}
}
}
class MyELClass {
public static void main(String args[]) {
try {
Employee e = new Employee();
e.decideSal(args[0]);
} catch (NumberFormatException nfe) {
System.err.println("Please enter integer values.");
} catch (NSalException no) {
System.err.println("Negative Salary.");
} catch (PSalException po) {
System.err.println("Valid Positive Salary.");
} finally {
System.out.println("Finally Block executing");
}
System.out.println("Gurkirat singh, 13313203122" );
}
}
Output:
Program – 14
Code:
import java.util.*;
class SortNames {
String name[] = new String[5];
int i, n = 5;
SortNames() {
for (i = 0; i < n; i++) {
name[i] = null;
}
}
void inputNames() {
name[0] = "Yogesh";
name[1] = "Deepak";
name[2] = "Aman";
name[3] = "Srishti";
name[4] = "Baljinder";
}
void bubbleSort() {
int j;
String temp;
for (i = 0; i < n; i++) {
for (j = 0; j < n - 1 - i; j++) {
if (name[j].compareTo(name[j + 1]) > 0) {
temp = name[j];
name[j] = name[j + 1];
name[j + 1] = temp;
}
}
}
}
void displayNames() {
for (i = 0; i < n; i++) {
System.out.println(name[i]);
}
}
}
class MyELClass {
public static void main(String a[]) {
SortNames sn = new SortNames();
sn.inputNames();
sn.bubbleSort();
sn.displayNames();
System.out.println("Gurkirat singh, 13313203122" );
}
}
Output:
Program – 15
Aim: Design a java program to create two threads: one for odd numbers and other for even
numbers.
Code:
void set() {
this.num = 0;
}
void even() {
num = num + 2;
System.out.println("Even : " + num);
}
}
void set() {
this.num = 1;
}
void odd() {
num = num + 2;
System.out.println("Odd : " + num);
}
}
class JavaLab {
public static void main(String a[]) {
int i;
EvenThread et = new EvenThread();
OddThread ot = new OddThread();
Thread t = new Thread(ot);
et.set();
ot.set();
et.start();
t.start();
System.out.println("Gurkirat singh, 13313203122" );
}
}
Output:
Program – 16
Aim: Design a java program to implement producer consumer problem using concept of
inter thread communication.
Code:
class Q {
int n;
boolean valueSet = false;
Producer(Q q) {
this.q = q;
new Thread(this, "Producer").start();
}
Consumer(Q q) {
this.q = q;
new Thread(this, "Consumer").start();
}
class MyELClass {
public static void main(String args[]) {
Q q = new Q();
new Producer(q);
new Consumer(q);
System.out.println("Press Control-C to stop.");
System.out.println("Gurkirat singh, 13313203122" );
}
}
Program – 17
Aim: Design a java program to create three threads runnable interface. First thread display
"GOOD MORNING" at every 1 second. Second thread displays "HELLO" at every 2
seconds and third thread displays "WELCOME" at every 3 seconds.
Code:
class Callme {
void call(String msg, int t) {
System.out.println(msg);
try {
Thread.sleep(t * 1000);
} catch (InterruptedException e) {
System.out.println("Interrupted");
}
}
}
class MyELClass {
public static void main(String args[]) {
Callme target = new Callme();
Caller ob1 = new Caller(target, "GOOD MORNING", 1);
Caller ob2 = new Caller(target, "HELLO", 2);
Caller ob3 = new Caller(target, "WELCOME", 3);
try {
ob1.t.join();
ob2.t.join();
ob3.t.join();
} catch (InterruptedException e) {
System.out.println("Interrupted");
}
System.out.println("Gurkirat singh, 13313203122" );
}
}
Output:
Program – 18
Aim: Design a java program to create three threads. First thread display "GOOD
MORNING" at every 1 second. Second thread displays "HELLO" at every 2 seconds and
third thread displays "WELCOME" at every 3 seconds.
Code:
Code:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
Output:
Program – 20
Aim: Design a java program to write an applet that computes the payment of loan which is
based on the amount of loan, interest rate and number of months. It takes one parameter
from browser which is called Monthly Rate. If it is True then interest rate is per month else
it is annual. (Use label, textfield, button, check box, checkbox group).
Code:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
loanAmount = Double.parseDouble(tf1.getText());
interestRate = Double.parseDouble(tf2.getText());
if (interestRate == 0 ) {
showStatus("Please enter valid interest rate and loan term." + loanAmount +
interestRate);
return;
}
if (cb1.getState()) {
interestRate /= 12/100;
}
else{
interestRate /= 100;
}
repaint();
}
}
public void paint(Graphics g) {
g.drawString("LOAN PAYMENT AMOUNT: $" + String.format("%.2f",
loanPayment), 20, 120);
}
}
Output:
Program – 21
Aim: Design a java program to write an applet with following AWT components :textarea
and button.
Code:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
Output:
Program – 22
Aim: Design a java program to write a calculator applet. Use grid layout to arrange the
buttons for digits for the +,-,*,/,% operations. Add text-field to display the result.
Code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
TextField result;
Button buttons[];
String operator = "";
double resultValue = 0;
if (operator.equals("+")) {
resultValue = number1 + number2;
} else if (operator.equals("-")) {
resultValue = number1 - number2;
} else if (operator.equals("*")) {
resultValue = number1 * number2;
} else if (operator.equals("/")) {
resultValue = number1 / number2;
} else if (operator.equals("%")) {
resultValue = number1 % number2;
}
result.setText(Double.toString(resultValue));
}
}
}
Output:
Program – 23
Code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
String message;
Output:
Program – 24
Code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
addButton.addActionListener(this);
subtractButton.addActionListener(this);
multiplyButton.addActionListener(this);
divideButton.addActionListener(this);
resultPanel.add(new Label("Result:"));
resultPanel.add(resultField);
cardPanel.add(inputPanel, "input");
cardPanel.add(resultPanel, "result");
add(cardPanel);
}
if (e.getSource() == addButton) {
result = num1 + num2;
} else if (e.getSource() == subtractButton) {
result = num1 - num2;
} else if (e.getSource() == multiplyButton) {
result = num1 * num2;
} else if (e.getSource() == divideButton) {
if (num2 != 0) {
result = num1 / num2;
} else {
resultField.setText("Error: Divide by zero");
return;
}
}
resultField.setText(Double.toString(result));
cardLayout.show(cardPanel, "result");
}
}
Output:
Program – 25
Code:
import java.io.*;
bufferedWriter.write("Hello, World!\n");
bufferedWriter.write("This is a Java program.\n");
bufferedWriter.write("123\n");
bufferedWriter.write("456\n");
bufferedWriter.close();
System.out.println("Data has been written to the file.");
} catch (IOException e) {
System.err.println("Error writing to the file: " + e.getMessage());
}
bufferedReader.close();
System.out.println("Gurkirat singh, 13313203122" );
} catch (IOException e) {
System.err.println("Error reading from the file: " + e.getMessage());
}