Decision Making & Switch Statement 1. Write a program to display the square and cube of a positive no. import java.util.*; public class hello { public static void main(String [] args) { Scanner in = new Scanner(System.in); System.out.println("Enter a No."); int n = in.nextInt(); if (n < 0) { System.out.println("It is a negative number"); System.exit(0); } int sq = n * n; int cb = n * n * n; System.out.println("Square of a number: " + sq); System.out.println("Cube of a number: " + cb); } } Sample Output: Enter a No. 12 Square of a number: 144 Cube of a number: 1728 2. Write a program to enter two unequal numbers. If the first number is greater then display square of the smaller number and cube of the greater number otherwise, vice-versa. If the numbers are equal, display the message "Both the numbers are equal". import java.util.*; public class hello { public static void main(String [] args) { Scanner in = new Scanner(System.in); System.out.println("Enter two numbers"); int a = in.nextInt(); int b = in.nextInt(); int sq, c; if (a != b) { if (a < b) { sq = a * a; c = b * b * b; } else { sq = b * b; c = a * a * a; } System.out.println("The square of smaller number:" + sq); System.out.println("The cube of greater number:" + c); } else { System.out.println("Both the numbers are equal"); } } } Sample Output: Enter two numbers 20 21 The square of smaller number:400 The cube of greater number:9261 3 Write a program to accept the length and breath of a rectangle. Calculate and display the area, perimeter or diagonal of the rectangle as per the user choice import java.util.*; public class switch_rectangle_12 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("Enter length, breadth of a rectangle"); int l = in.nextInt(); int b = in.nextInt(); System.out.println("Enter 1 for area, 2 for perimeter, 3 for diagonal"); int ch = in.nextInt(); switch (ch) { case 1: int a = l * b; System.out.println(a); break; case 2: int p = 2 * (l + b); System.out.println(p); break; case 3: double d = Math.sqrt(l * l + b * b); System.out.println(d); break; default: System.out.println("Invalid entry"); } } } Sample Output: Enter length, breadth of a rectangle 120 120 Enter 1 for area, 2 for perimeter, 3 for diagonal 2 480 4 . Write a program to accept two angles. Calculate and display whether they are ‘Complementary Angles’ or ‘Supplementary’ Angles as per the user’s choice. [Hint: Enter 'c’ for complementary or ‘s’ for supplementary] import java.util.*; public class Dkangle_13 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("Enter two angles"); int a = in.nextInt(); int b = in.nextInt(); int sum = a + b; System.out.println("Enter 'c' for Complementary, 's' for Supplementary"); System.out.println("Enter your choice"); char ch = in.next().charAt(0); switch (ch) { case 'c': if (sum == 90) { System.out.println("Complementary Angles"); } else { System.out.println("Not Complementary Angles"); } break; case 's': if (sum == 180) { System.out.println("Supplementary Angles"); } else { System.out.println("Not Supplementary Angles"); } break; default: System.out.println("Wrong Choice"); } } } Sample Output: Enter two angles 60 30 Enter 'c' for Complementary, 's' for Supplementary Enter your choice c Complementary Angles Iteration through Loop i. For loop statement (Single and nested) 1. Write a program to print 20 terms of series 3n+2.Stop as soon as multiple of 4 is encountered public class series_28_93 { public static void main() { for(int i=1;i<=20;i++) { int a=3*i+2; if(a%4==0) { break; } System.out.println(a); } } } Sample Output: 5 2. Write a program to print the first 20 member of the fibonacciseries ; 0, 1, 1, 2, 3, 5, 8, ………………. public class fibonacii1_73_95 { public static void main() { int a, b; a = 0; b = 1; System.out.println(a); System.out.println(b); for (int x = 1; x <= 18; x++) { int c = a + b; System.out.println(c); a = b; b = c; } } } Sample Output: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 3. Pattern Program (star1) * *** ***** ******* ********* ******* ***** *** * import java.util.Scanner; public class Star1 { public static void main(String args[]) { int n, i, j, space = 1; System.out.print("Enter the number of rows: "); Scanner s = new Scanner(System.in); n = s.nextInt(); space = n - 1; for (j = 1; j <= n; j++) { for (i = 1; i <= space; i++) { System.out.print(" "); } space--; for (i = 1; i <= 2 * j - 1; i++) { System.out.print("*"); } System.out.println(""); } space = 1; for (j = 1; j <= n - 1; j++) { for (i = 1; i <= space; i++) { System.out.print(" "); } space++; for (i = 1; i <= 2 * (n - j) - 1; i++) { System.out.print("*"); } System.out.println(""); } } } Sample Output: Enter the number of rows: 4 * *** ***** ******* ***** *** * 4. Pattern Program 2 (RT1) * * * * * * * * * * * * * * * import java.util.Scanner; public class RT1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter the number of rows: "); int rows = sc.nextInt(); for (int i = rows - 1; i >= 0; i--) { for (int j = 0; j <= i; j++) { System.out.print("*" + " "); } System.out.println(); } sc.close(); } } Sample Output: Enter the number of rows: 4 **** *** ** * ii. While & do while loop (Single Loop) 1.Write a program to enter nos. till the user wants and at the end it should display the count of positive, negative and zeros. import java.util.*; public class positive_negative_24_78 { public static void main() { Scanner in = new Scanner(System.in); int p = 0, ne = 0, z = 0, ch; while (true) { System.out.println("Enter a no."); int n = in.nextInt(); if (n > 0) { p++; } else if (n < 0) { ne++; } else { z++; } System.out.println("Enter 1 to continue and 0 to exit"); ch = in.nextInt(); if (ch == 0) { break; } } System.out.println(“positive number=”+p); System.out.println(“negative number=”+ne); System.out.println(“zeros=”+z); } } Output: Enter a no. 1 Enter 1 to continue and 0 to exit 1 Enter a no. -1 Enter 1 to continue and 0 to exit 1 Enter a no. 0 Enter 1 to continue and 0 to exit 0 positive number=1 negative number=1 zeros=1 2. Write a program to enter nos. till 0 is entered and print sum and average. import java.util.*; public class Josheph_49_79 { public static void main() { Scanner in = new Scanner(System.in); int s = 0, c = 0; while (true) { System.out.println("Enter a no.(0 to exit)"); int n = in.nextInt(); if (n == 0) { break; } s = s + n; c++; } double avg = (double) s / c; System.out.println("äverage=" avg); } } Sample Output: Enter a no.(0 to exit) 12 Enter a no.(0 to exit) 12 Enter a no.(0 to exit) 23 Enter a no.(0 to exit) 0 äverage=15.666666666666666 3. Write a program to print the sum of negative numbers, sum of positive even numbers and sum of positive odd numbers from a list of numbers (N) entered by the User. The list terminates when the User enters a zero. import java.util.*; public class hello { public static void main(String[]args) { Scanner in = new Scanner(System.in); int sn = 0, spe = 0, spo = 0; int n; do { System.out.println("Enter a no."); n = in.nextInt(); if (n < 0) { sn = sn + n; } if (n > 0 && n % 2 == 0) { spe = spe + n; } if (n > 0 && n % 2 != 0) { spo = spo + n; } } while (n != 0); System.out.println("Sum of negative number"+sn); System.out.println("Sum of positive even number"+spe); System.out.println("Sum of positive odd number"+spo); } } Output: Enter a no. 12 Enter a no. 12 Enter a no. -1 Enter a no. -12 Enter a no. 0 Sum of negative number-13 Sum of positive even number24 Sum of positive odd number0 4.Write a Program to print values from 1 to 10 public class hello { public static void main(String[] args) { int i = 1; do { System.out.println(i); i++; } while (i <= 10); } } 1 2 3 4 5 6 7 8 9 10 1. Define a class salary described as below: a) Data Members : name, address, phone, subject, specialization, monthly salary, income tax b) Member methods : i) To accept the details of a teacher including the monthly salary ii) To display the details of the teacher iii) To compute the annual income tax as 5% of the annual salary above Rs.1,75,000/Write a main method to create the object of the class and call the above member method. [2007] public class hello { String name; String address; long phone; String subject; String specialization; int monthly_salary; double income_tax; public hello(String n, String a, long p, String s, String sp, int ms) { name = n; address = a; phone = p; subject = s; specialization = sp; monthly_salary = ms; } public void display() { System.out.println("name="+name); System.out.println("address="+address); System.out.println("phone="+phone); System.out.println("subject="+subject); System.out.println("specialization="+specialization); System.out.println("monthly_salary="+monthly_salary); System.out.println("income_tax="+income_tax); } public void calculate() { int ai = monthly_salary * 12; if (ai > 175000) { income_tax = 0.05 * (ai - 175000); } } public static void main(String[] args) { hello k = new hello("Rabi", "jobra", 9437277561L, "physics", "astronomy", 20000); k.calculate(); k.display(); } } Sample Output: name=Rabi address=jobra phone=9437277561 subject=physics specialization=astronomy monthly_salary=20000 income_tax=3250.0 2. Write a java class MyClass. The description of the class as follows a) Instance variable : to store the age of a student. b) Constructor : to initialize age by 14. c) main method – to create the object and display the result. public class hello { int age; public hello() { age = 14; } public static void main() { hello k = new hello(); System.out.println(k.age); } } Sample Output: 14 Function Overloading 1. Write a program with three methods with name Sum to find the sum of numbers inputted public class OverloadedMethod { public int Sum(int x, int y) { return (x + y); } public int Sum(int x, int y, int z) { return (x + y + z); } public double Sum(double x, double y) { return (x + y); } public static void main(String args[]) { OverloadedMethod s = new OverloadedMethod(); System.out.println(s.Sum(10, 20)); System.out.println(s.Sum(10, 20, 30)); System.out.println(s.Sum(10.5, 20.5)); } } Sample Output: 30 60 31 2.Write a program with three methods with name multiply to find the multiplication of numbers inputted class Adder { public int multiply(int a,int b) { return a*b; } public int multiply(int a,int b,int c) { return a*b*c; } public static void main(String[] args) { Adder x = new Adder(); System.out.println(x.multiply(110,110)); System.out.println(x.multiply(110,110,110)); } } Sample Output: 12100 1331000 3. Write a program with three methods with name divide to find the division of numbers inputted class Div { public int divide(int a, int b) { return a/b; } public int divide(int a, int b, int c) { return ((a / b)/c); } public static void main(String[] args) { Div x = new Div(); System.out.println(x.divide(110, 110)); System.out.println(x.divide(1100, 11, 10)); } } Sample Output: 1 10 String Handling Write a Program to convert a string into upper Case class StringUpperExample { public static void main(String args[]) { String s1 = "hello string"; String s1upper = s1.toUpperCase(); System.out.println(s1upper); } } Sample Output: HELLO STRING Write a program to remove space from both ends of string class StringTrimExample { public static void main(String args[]) { String s1 = " hello string "; System.out.println(s1 + "java");//without trim() System.out.println(s1.trim() + "javac");//with trim() } } Sample Output: hello string java hello stringjavac Write a program to see two strings are equal without matching the case class EqualsIgnoreCaseExample { public static void main(String args[]) { String s1 = "javatpoint"; String s2 = "javatpoint"; String s3 = "JAVATPOINT"; String s4 = "python"; System.out.println(s1.equalsIgnoreCase(s2));//true because content and case both are same System.out.println(s1.equalsIgnoreCase(s3));//true because case is ignored System.out.println(s1.equalsIgnoreCase(s4));//false because content is not same } } Sample Output: true true false Array i. Single Dimensional Array 1. Write a Program to find Minimum and Maximum Values in an array class a_min_max_sum_19 { public static void main(String []args) { int a[] = {2, 5, 4, 1, 3}; int min = a[0], max = a[0], sum = 0; for (int x = 0; x < 5; x++) { if (a[x] > max) { max = a[x]; } if (a[x] < min) { min = a[x]; } sum = sum + a[x]; } System.out.println("Maximum value= " + max); System.out.println("Minimum value= " + min); System.out.println("Sum of the elements= " + sum); } } Sample Output: Maximum value= 5 Minimum value= 1 Sum of the elements= 15 2.Write a program to find highest marks among marks entered import java.util.*; import java.util.*; class a_avg_highest_mark_20 { public static void main(String[] args) { Scanner in = new Scanner(System.in); String n[] = new String[50]; int m[] = new int[50]; for (int x = 0; x < 50; x++) { System.out.println("Enter name"); n[x] = in.nextLine(); System.out.println("Enter Mark"); String k = in.nextLine(); m[x] = Integer.parseInt(k); } int max = 0, sum = 0; String maxn = ""; for (int y = 0; y < 50; y++) { sum = sum + m[y]; if (m[y] > max) { max = m[y]; maxn = n[y]; } } double avg = sum / 50.0; System.out.println("Average marks =" + avg); System.out.println("Highest marks =" + max); System.out.println("Student who got the higest= " + maxn); } } Sample Output: Enter name star Enter Mark 12 Enter name khan Enter Mark 12 Enter name hei Enter Mark 40 Enter name bhei Enter Mark 50 Enter name sei Enter Mark 50 Average marks =32.8 Highest marks =50 Student who got the higest= bhei Write a program to take input of marks of students and rate them import java.util.*; class a_mark2_21 { public static void main(String []args) { Scanner in = new Scanner(System.in); System.out.println("Enter number of Students"); int n = Integer.parseInt(in.nextLine()); int r[] = new int[n]; String nm[] = new String[n]; int m[] = new int[n]; int s[] = new int[n]; int e[] = new int[n]; double avg; String str = ""; for (int a = 0; a < n; a++) { System.out.println("Enter roll number"); r[a] = Integer.parseInt(in.nextLine()); System.out.println("Enter the name"); nm[a] = in.nextLine(); System.out.println("Enter marks in three subjects"); m[a] = Integer.parseInt(in.nextLine()); s[a] = Integer.parseInt(in.nextLine()); e[a] = Integer.parseInt(in.nextLine()); } for (int x = 0; x < n; x++) { avg = (m[x] + e[x] + s[x]) / 3.0; if (avg < 40) { str = "POOR"; } else if (avg >= 40 && avg <= 59) { str = "PASS"; } else if (avg >= 60 && avg <= 74) { str = "FIRST CLASS"; } else if (avg >= 75 && avg <= 84) { str = "DISTINCTION"; } else { str = "EXCELLENT"; } System.out.println(nm[x] + "\t" + avg + "\t" + str); } } } Sample Output: Enter number of Students 2 Enter roll number 1 Enter the name adwait Enter marks in three subjects 12 13 15 Enter roll number 34 Enter the name manoj Enter marks in three subjects 12 46 10 adwait 13.333333333333334 POOR manoj 22.666666666666668 POOR ii. Double Dimensional Array 1. GFG Create and Initialize a 2d array class class GFG { public static void main(String[] args) { int[][] arr = {{1, 2}, {3, 4}} for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) System.out.println("arr[" + i + "][" + j + "] = " + arr[i][j]); } } Sample Output: arr[0][0] = 1 arr[0][1] = 2 arr[1][0] = 3 arr[1][1] = 4 2. GG Create and Print elements of a 2d Array class GG { public static void main(String[] args) { int[][] oddNumbers = {{1, 3, 5, 7}, {9, 11, 13, 15}, {17, 19, 21, 23}}; for (int i = 0; i < oddNumbers.length; i++) { for (int j = 0; j < oddNumbers[i].length; j++) { System.out.println(oddNumbers[i][j]); } } } } Sample Output: 1 3 5 7 9 11 13 15 17 19 21 23 3. GE Create and Print a Matrix class Print2DArray { public static void main(String[] args) { final int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[i].length; j++) { /this equals to the column in each row. System.out.print(matrix[i][j] + " "); } System.out.println(); } } } Sample Output: 123 456 789