You are on page 1of 18

Program 17. Write a Java program to determine greatest number of three numbers.

1. import java.util.Scanner;
2. public class LargestNumberExample1
3. {
4. public static void main(String[] args)
5. {
6. int a, b, c, largest, temp;
7. //object of the Scanner class
8. Scanner sc = new Scanner(System.in);
9. //reading input from the user
10. System.out.println("Enter the first number:");
11. a = sc.nextInt();
12. System.out.println("Enter the second number:");
13. b = sc.nextInt();
14. System.out.println("Enter the third number:");
15. c = sc.nextInt();
16. //comparing a and b and storing the largest number in a temp variable
17. temp=a>b?a:b;
18. //comparing the temp variable with c and storing the result in the variable
19. largest=c>temp?c:temp;
20. //prints the largest number
21. System.out.println("The largest number is: "+largest);
22. }
23. }
Program 18. Write a Java program to determine smallest number of three numbers.

1. import java.util.Scanner;
2. public class SmallestNumberExample1
3. {
4. public static void main(String[] args)
5. {
6. int a, b, c, smallest, temp;
7. //object of the Scanner class
8. Scanner sc = new Scanner(System.in);
9. //reading input from the user
10. System.out.println("Enter the first number:");
11. a = sc.nextInt();
12. System.out.println("Enter the second number:");
13. b = sc.nextInt();
14. System.out.println("Enter the third number:");
15. c = sc.nextInt();
16. //comparing a and b and storing the smallest number in a temp variable
17. temp=a<b?a:b;
18. //comparing the temp variable with c and storing the result in the variable names smallest
19. smallest=c<temp?c:temp;
20. //prints the smallest number
21. System.out.println("The smallest number is: "+smallest);
22. }
23. }

Output:

Enter the first number:


23
Enter the second number:
11
Enter the third number:
67
The smallest Number is: 11

Program 19. Compute the average of three numbers through a java program.

import java.util.Scanner;

public class JavaExample {

public static void main(String[] args)


{
Scanner scan = new Scanner(System.in);

System.out.print("Enter the first number: ");


double num1 = scan.nextDouble();
System.out.print("Enter the second number: ");
double num2 = scan.nextDouble();
System.out.print("Enter the third number: ");

double num3 = scan.nextDouble();


scan.close();
System.out.print("The average of entered numbers is:" + avr(num1, num2, num3) );
}

public static double avr(double a, double b, double c)


{
return (a + b + c) / 3;
}
}
Program 20 . Write a program to perform following operations on strings:
1) Compare two strings.
2) Count string length.
3) Convert upper case to lower case & vice versa.
4) Concatenate two strings.
5) Print a substring.

class stringop
{
public static void stringCompare(String s1, String s2, String s3, String s4)
{
System.out.println("Comparing " + s1 + " and " + s2
+ " : " + s1.equals(s2));
System.out.println("Comparing " + s1 + " and " + s3
+ " : " + s1.equals(s3));
System.out.println("Comparing " + s2 + " and " + s3
+ " : " + s2.equals(s3));
System.out.println("Comparing " + s2 + " and " + s4
+ " : " + s2.equals(s4));
}
public static void countLength(String s1, String s2, String s3, String s4)
{
System.out.println("Length of " + s1 + " is " + s1.length());
System.out.println("Length of " + s2 + " is " + s2.length());
System.out.println("Length of " + s3 + " is " + s3.length());
System.out.println("Length of " + s4 + " is " + s4.length());
}
public static void changecaselower(String s1)
{
System.out.println("lower case output is "+ s1.toLowerCase());
}
public static void changecaseupper(String s1)
{
System.out.println("upper case output is "+ s1.toUpperCase());
}
public static void joinstring(String s1, String s2)
{

String s3=s1.concat(s2);
System.out.println("The concatinated string is "+s3);
}
public static void Substr(String s1)
{
System.out.print("The extracted substring is : ");
System.out.println(s1.substring(4,8));
}
}
class Main {
public static void main(String[] args) {
// create strings
String first = "Java";
String second = "Python";
String third = "JavaScript";
String four ="Python";

// print strings
System.out.println(first); // print Java
System.out.println(second); // print Python
System.out.println(third); // print JavaScript
stringop ob1 = new stringop();
ob1.stringCompare(first,second,third, four);
ob1.countLength(first,second,third, four);
ob1.changecaselower(third);
ob1.changecaseupper(second);
ob1.joinstring(first,second);
ob1.Substr(third);
}
}
Program 21 . Write a Program & design a method to count all vowels in a string.

import java.util.Scanner;
public class CountingVowels {
public static void main(String args[]){
int count = 0;

System.out.println("Enter a sentence :");


Scanner sc = new Scanner(System.in);
String sentence = sc.nextLine();

for (int i=0 ; i<sentence.length(); i++){


char ch = sentence.charAt(i);
if(ch == 'a'|| ch == 'e'|| ch == 'i' ||ch == 'o' ||ch == 'u'||ch == ' '){
count ++;

}
}
System.out.println("Number of vowels in the given sentence is "+count);
}
}

Program 22. Write a Java method to count all words in a string.

public class Example1 {

public static void main(String args[]) {


// initializing a string
String msg = "Tutorials Point Welcomes You!!";
System.out.println("The given String is: " + msg);
// initial count of the words

int total = 1;
// loop variable
int i = 0;
// while loop to count the number of words
while (i < msg.length()) {
// checking if the current character is space or not
if ((msg.charAt(i) == ' ') && (msg.charAt(i + 1) != ' ')) {
total++; // incrementing the word count
}
i++; // incrementing loop variable
}

// printing the result


System.out.println("Number of words in the given string: " + total);
}
}

Program 23. To represent the concept of all types of inheritance supported by Java,
design a program.
Single Inheritance:
class Student {
void Play() {
System.out.println("Playing Fooball...");
}
}
class Bob extends Student {
void Study() {
System.out.println("Studing Physics...");
}
}
class Single {
public static void main(String args[]) {
Bob d = new Bob();
d.Study();
d.Play();
}
}

Multilevel inheritance:

1. class Animal{
2. void eat(){System.out.println("eating...");}
3. }
4. class Dog extends Animal{
5. void bark(){System.out.println("barking...");}
6. }
7. class BabyDog extends Dog{
8. void weep(){System.out.println("weeping...");}
9. }
10. class TestInheritance2{
11. public static void main(String args[]){
12. BabyDog d=new BabyDog();
13. d.weep();
14. d.bark();
15. d.eat();
16. }}
Hierarchical Inheritance:

class A {

public void print_A() { System.out.println("Class A"); }

class B extends A {

public void print_B() { System.out.println("Class B"); }

class C extends A {

public void print_C() { System.out.println("Class C"); }

class D extends A {

public void print_D() { System.out.println("Class D"); }

// Driver Class
class Test {

public static void main(String[] args)

B obj_B = new B();

obj_B.print_A();

obj_B.print_B();

C obj_C = new C();

obj_C.print_A();

obj_C.print_C();

D obj_D = new D();

obj_D.print_A();

obj_D.print_D();

Program 24. Write a Java program to handle the following exceptions:


1) Divide by Zero Exception.
2) Array Index Out Of bound Exception.
public class NewClass2 {
public static void main(String[] args)
{
int ar[] = { 0, 1, 2, 3, 4 };
try
{
for (int i = 0; i <= ar.length; i++)
System.out.print(ar[i]+" ");
}
catch (Exception e)
{
System.out.println("\n out of bound Exception caught");
}
for (int i = 0; i < ar.length; i++)
try
{
System.out.println(ar[i+1] /ar[i]);
}
catch (ArithmeticException e)
{
System.out.println("Divided by zero operation cannot possible");
}
}
}
Program 25. Write a program to implement Multiple Inheritance using interface.
interface Walkable {
void walk();
}

interface Swimmable {
void swim();
}

// Implement the interfaces in a class


class Duck implements Walkable, Swimmable {
public void walk()
{
System.out.println("Duck is walking.");
}

public void swim()


{
System.out.println("Duck is swimming.");
}
}

// Use the class to call the methods from the interfaces


class Main {
public static void main(String[] args)
{
Duck duck = new Duck();
duck.walk();
duck.swim();
}
}

Program 26. Construct a program to design a package in Java.


OUTPUT:
13
3

Program 27. To represent the concept of Multithreading write a Java program.


OUTPUT:
From Thread A : i = 1
From Thread B : j = 1
From Thread B : j = 2
From Thread C : k = 1
From Thread C : k = 2
From Thread B : j = 3
From Thread B : j = 4
From Thread B : j = 5
Exit from B
From Thread A : i = 2
From Thread A : i = 3
From Thread A : i = 4
From Thread A : i = 5
Exit from A
From Thread C : k = 3
From Thread C : k = 4
From Thread C : k = 5
Exit from C

You might also like