Java 3
Java 3
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:
Program 19. Compute the average of three numbers through a java program.
import java.util.Scanner;
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("Number of vowels in the given sentence is "+count);
}
}
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
}
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 {
class B extends A {
class C extends A {
class D extends A {
// Driver Class
class Test {
obj_B.print_A();
obj_B.print_B();
obj_C.print_A();
obj_C.print_C();
obj_D.print_A();
obj_D.print_D();
interface Swimmable {
void swim();
}