0% found this document useful (0 votes)
11 views

Java Micro

The document contains multiple Java programming exercises, including converting Fahrenheit to Celsius, finding the greatest of three numbers, summing an array, and performing basic arithmetic operations on two integers. It also covers method overloading, overriding, abstract classes, interfaces, exception handling, and data structures like ArrayList, Set, and Queue. Additionally, it includes examples of event delegation and thread synchronization in Java.

Uploaded by

Balaji Gorusu
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)
11 views

Java Micro

The document contains multiple Java programming exercises, including converting Fahrenheit to Celsius, finding the greatest of three numbers, summing an array, and performing basic arithmetic operations on two integers. It also covers method overloading, overriding, abstract classes, interfaces, exception handling, and data structures like ArrayList, Set, and Queue. Additionally, it includes examples of event delegation and thread synchronization in Java.

Uploaded by

Balaji Gorusu
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
You are on page 1/ 3

1) Write a Java program to convert temperature from Fahrenheit to Celsius degrees 3) Write a Java program that takes three

rogram that takes three numbers from the user and prints the greatest number. 4) Write a Java program to sum values of an array.

import java.u l.Scanner; public class Exercise {


import java.u l.Scanner;
public class Exercise1 public class Exercise public sta c void main(String[] args) {
{
{ int my_array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
public sta c void main(String[] Strings)
{ public sta c void main(String[] args) int sum = 0;
Scanner input = new Scanner(System.in); { for (int i : my_array)
System.out.print("Input a degree in Fahrenheit: ");
double fahrenheit = input.nextDouble(); Scanner in = new Scanner(System.in); sum += i;
double celsius =(( 5 *(fahrenheit - 32.0)) / 9.0); System.out.print("Input the 1st number: "); System.out.println("The sum is " + sum);
System.out.println(fahrenheit + " degree Fahrenheit is equal to " + celsius + " in Celsius");
} int num1 = in.nextInt(); }
} }

2) Write a Java program that accepts two integers from the user and prints the sum, the difference, the System.out.print("Input the 2nd number: "); 5) write a java program on method overloading by crea ng a sample class, in that create two methods, first
product, the average, the distance (the difference between the integers), the maximum (the largest add () method performs addi on of two numbers and second add() performs addi on of three numbers.
int num2 = in.nextInt();
of the two integers), and the minimum (the smallest of the two integers). class Sum {
public int sum(int x, int y) {
import java.u l.Scanner; System.out.print("Input the 3rd number: ");
public class Exercise { return (x + y);
int num3 = in.nextInt();
public sta c void main(String[] args) }
{
public int sum(int x, int y, int z) {
Scanner in = new Scanner(System.in);
if (num1 > num2) return (x + y + z);
System.out.print("Input 1st integer: ");
int firstInt = in.nextInt(); if (num1 > num3) }
System.out.print("Input 2nd integer: ");
System.out.println("The greatest: " + num1); public double sum(double x, double y)
int secondInt = in.nextInt();
System.out.prin ("Sum of two integers: %d%n", firstInt + secondInt); {
System.out.prin ("Difference of two integers: %d%n", firstInt - secondInt); return (x + y);
if (num2 > num1)
System.out.prin ("Product of two integers: %d%n", firstInt * secondInt);
}
System.out.prin ("Average of two integers: %.2f%n", (double) (firstInt + secondInt) / 2); if (num2 > num3)
System.out.prin ("Distance of two integers: %d%n", Math.abs(firstInt - secondInt)); public sta c void main(String args[])
System.out.println("The greatest: " + num2);
System.out.prin ("Max integer: %d%n", Math.max(firstInt, secondInt)); {
System.out.prin ("Min integer: %d%n", Math.min(firstInt, secondInt));
Sum s = new Sum();
} if (num3 > num1)
} System.out.println(s.sum(10, 20));
if (num3 > num2)
System.out.println(s.sum(10, 20, 30));
System.out.println("The greatest: " + num3); System.out.println(s.sum(10.5, 20.5));
} }
} }

6) write a program on method overriding by defining the run method in the subclass as defined in the parent ANOTHER CODE FOR 6 TH QUSTION 7) Write a Java program on mul ple inheritance by to crea ng an interface DisplayAge with the displayAge()
class but it has some specific implementa on. The name and parameter of the method are the same, and method. create an interface DisplayName with the displayName () method. Create classes Test that
class Parent
there should be IS-A rela onship between the classes. implement the DisplayAge interface and DisplayName .Implement the displayAge() method and
{ displayName () method.
class Parent {
void show() interface DisplayAge
void show()
{ {
{
System.out.println("Parent's show()"); void displayAge(int age);
System.out.println("Parent's show()");
} }
}
} interface DisplayName
}
class Child extends Parent { {
class Child extends Parent {
void show() void displayName(String name);
void show()
{ }
{
System.out.println("Child's show()"); public class Test implements DisplayAge, DisplayName
System.out.println("Child's show()");
} {
}
} public void displayAge(int age)
}
class Main { {
class Main {
public sta c void main(String[] args) System.out.println("Age = " + age);
public sta c void main(String[] args)
{ }
{
Child obj1 = new Child();
Parent obj1 = new Parent();
obj1.show(); public void name(String name)
obj1.show();
} {
Parent obj2 = new Child();
} System.out.println("Name = " + name);
obj2.show();
}
}
public sta c void main(String args[])
}
{

Test obj = new Test();

obj.name("prem williams");

obj.age(27);

}
8) Write a Java program to create an abstract class Animal with an abstract method called sound(). Create 9) write a program on String class in Java. 10A) implement a Java program on ArrayList in Java?
subclasses Lion and Tiger that extend the Animal class and implement the sound() method to make a
import java.io.*; import java.u l.ArrayList;
specific sound for each animal.
import java.u l.*; public class ArrayListDemo {
Abstract class Animal
class Test public sta c void main(String[] args) {
abstract class Animal {
{ ArrayList languages = new ArrayList<>();
public abstract void sound();
public sta c void main (String[] args) languages.add("Java");
}
{ languages.add("Python");
class Lion extends Animal {
String s= "welcome to our college"; languages.add("C++");
public void sound() {
// or String s= new String ("welcome to our college "); System.out.println("Languages: " + languages);
System.out.println("Lion roars!");
String secondLang = languages.get(1);
}
System.out.println("String length = " + s.length()); System.out.println("Element at index 1: " + secondLang);
}
System.out.println("Character at 3rd posi on = "+ s.charAt(3)); languages.set(2, "C#");
class Tiger extends Animal {
System.out.println("Substring " + s.substring(3)); System.out.println("Updated list: " + languages);
public void sound() {
System.out.println("Substring = " + s.substring(2,5)); languages.remove("Java");
System.out.println("Tiger growls!");
String s1 = "Hello"; System.out.println("A er removing 'Java': " + languages);
}
String s2 = "How are you??"; System.out.println("Total languages: " + languages.size());
}
System.out.println("Concatenated string = " +s1.concat(s2)); }
public class Main {
String s4 = "Learn Share Learn"; }
public sta c void main(String[] args) {
System.out.println("Index of Share " +s4.indexOf("Share")); 10B) Implement a java program on Set interface ?
Animal lion = new Lion();
System.out.println("Index of a = " +s4.indexOf('a',3)); import java.u l.HashSet;
lion.sound(); // Output: Lion roars!
String word1 = "Hello…..Hi"; import java.u l.Set;

System.out.println("Changing to lower Case " +word1.toLowerCase()); public class SetDemo {


Animal ger = new Tiger();
String word2 = " Hello…..Hi"; public sta c void main(String[] args) {
ger.sound(); // Output: Tiger growls!
System.out.println("Changing to UPPER Case " +word2.toUpperCase()); Set fruits = new HashSet<>();
}
fruits.add("Apple");
}
} fruits.add("Banana");

} fruits.add("Mango");

fruits.add("Banana");

System.out.println("Fruits Set: " + fruits);

fruits.remove("Apple"); 11) Implement Excep on Handling in Java. fw.write(i);

System.out.println("A er removing Apple: " + fruits); class JavaExcep onExample }

boolean hasMango = fruits.contains("Mango"); { fw.flush();

System.out.println("Set contains Mango: " + hasMango); public sta c void main(String args[]) fw.close();

System.out.println("Total fruits: " + fruits.size()); { fr.close();

} try System.out.println(“data was copied successfully…..!!!!!");

} { }

10C) Implement a java program on Queue interface ? int data=100/0; catch(Excep on e)

import java.u l.LinkedList; } {

import java.u l.Queue; catch(Arithme cExcep on e) System.out.println(e);

public class QueueDemo { }

{ System.out.println(e); }

public sta c void main(String[] args) } }

{ System.out.println("rest of the code..."); 13) implement event delega on model in java using AWT pakage.

Queue <String>orders = new LinkedList<>(); } import java.awt.*;

orders.offer("Order 1"); } import java.awt.event.*;

orders.offer("Order 2"); 12). write a program to implement serializa on in Java class EDM implements Ac onListener

orders.offer("Order 3"); import java.io.*; {

System.out.println("Current Queue: " + orders); class Sample1 implements Serializable TextField t;

System.out.println("Next to process: " + orders.peek()); { EDM()

orders.poll(); public sta c void main(String args[]) {

System.out.println("A er processing one order: " + orders); { Frame f=new Frame("EDM Example");

System.out.println("Remaining orders: " + orders.size()); try f.setSize(500,500);

} { f.setVisible(true);

} FileReader fr=new FileReader("data.txt"); t=new TextField();

FileWriter fw=new FileWriter("copy.txt"); t.setBounds(60,50,180,25);

int i=0; f.add(t);

while((i=fr.read())!=-1) Bu on b=new Bu on("click Here");

{ b.setBounds(100,120,80,30);
f.add(b); }

b.addAc onListener(this); class TestSynchroniza on1

} } {

public void ac onPerformed(Ac onEvent e) } public sta c void main(String args[])

{ {

t.setText("hello my world!!!!!!!"); class MyThread1 extends Thread Table obj = new Table();

} { MyThread1 t1=new MyThread1(obj);

} Table t; MyThread2 t2=new MyThread2(obj);

public sta c void main(String args[]) MyThread1(Table t) t1.start();

{ { t2.start();

EDM e=new EDM(); this.t=t; }

} } }

} public void run()

14) implement thread synchroniza on in Java program. {

class Table t.printTable(5);

{ }

synchronized void printTable(int n) }

{ class MyThread2 extends Thread

for(int i=1;i<=5;i++) {

{ Table t;

System.out.println(n*i); MyThread2(Table t)

try {

{ this.t=t;

Thread.sleep(400); }

} public void run()

catch(Excep on e) {

{ t.printTable(100);

System.out.println(e); }

} }

You might also like