Java Micro
Java Micro
rogram that takes three numbers from the user and prints the greatest number. 4) Write a Java program to sum values of an array.
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[])
}
{
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;
} fruits.add("Mango");
fruits.add("Banana");
System.out.println("Set contains Mango: " + hasMango); public sta c void main(String args[]) fw.close();
} { }
{ System.out.println(e); }
{ System.out.println("rest of the code..."); 13) implement event delega on model in java using AWT pakage.
orders.offer("Order 2"); 12). write a program to implement serializa on in Java class EDM implements Ac onListener
System.out.println("A er processing one order: " + orders); { Frame f=new Frame("EDM Example");
} { f.setVisible(true);
{ b.setBounds(100,120,80,30);
f.add(b); }
} } {
{ {
t.setText("hello my world!!!!!!!"); class MyThread1 extends Thread Table obj = new Table();
{ { t2.start();
} } }
{ }
for(int i=1;i<=5;i++) {
{ Table t;
System.out.println(n*i); MyThread2(Table t)
try {
{ this.t=t;
Thread.sleep(400); }
catch(Excep on e) {
{ t.printTable(100);
System.out.println(e); }
} }