Posts

Understanding Enum using SWITCH CASE

Image
CODE enum Level { //use uppercase for first letter          LOW,MEDIUM,HIGH; } class apples{          public static void main (String args[]) {                 Level myvar = Level.HIGH; //learn this syntax to access ENUM constants                 switch (myvar) {         case LOW :             System.out.println("Low Level");             break;                     case MEDIUM:                System.out.println("Medium Level");             break;                 case HIGH:             System.out.println("High Level");             break;         }                 System.out.println(myvar); //this is the way to print constant of ENUM     } } OUTPUT REFERENCE https://www.w3schools.com/java/java_enums.asp https://www.youtube.com/watch?v=LYKHxwQ0QH8

Q Make a calculator that rounds off integerst to nearest 1000s, 100s and 10s as per requirement by user.

CODE import java.util.Scanner; import java.util.Random; public class red {     public static void main (String args[]) {         int n,a,cases;         Scanner bucky = new Scanner(System.in);         System.out.println("Enter your number");         n = bucky.nextInt();              System.out.println("What type of round off do you want?");         System.out.println("1. to nearest 10.");         System.out.println("2. to nearest 100.");         System.out.println("3. to nearest 1000.");         cases = bucky.nextInt();                  switch(cases) {                  case 1 :         a = n%10;         if(a>5) {             n = n + (10-a);             System.out.println("Your number is " + n);         } else {             n = n -a;             System.out.println("Your number is " + n);         }         break;                  case 2 :             a = n%100;             if(a>50) {                 n = n + (100-a

Taking Array Input

Image
CODE  import java.util.Scanner; public class orange {     public static void main (String args[]) {     int test[] = new int[15];     int n;     Scanner bucky = new Scanner(System.in);          System.out.println("How many elements you want in your array");     n = bucky.nextInt();          System.out.println("Enter the elements of array");          for(int i=0;i<n;i++) {         test[i] = bucky.nextInt();         // array input ke liye hum for loop use karte hai     }          System.out.println("Your array is ");     for(int j =0;j<n;j++) {         System.out.println(test[j]);     } } } OUTPUT

Finding index of an Array Element

Image
CODE import java.util.Scanner; import java.util.Random; public class red {     public static void main (String args[]) {     Scanner bucky = new Scanner(System.in);         int array[] = {25,14,96,35,12};     int number, h=0;          System.out.println("Your array is");     for(int i : array) {         System.out.printf(i + "  ");     }          System.out.println();     System.out.println("Now choose a number whose index you want to know");     number = bucky.nextInt();          for(int j=0;j<array.length;j++) {         if(number == array[j]) {             h=1;             System.out.println("The number is present in the defined array and its index is " + j);             break;         }     }          if(h==0) {         System.out.println("We cannot find the number inside");     }     } } OUTPUT A. WHEN NUMBER IS PRESENT B. WHEN NUMBER IS NOT PRESENT

Array Elements as Counters

Image
CODE import java.util.Random; public class red {     public static void main (String args[]) {                 Random rand = new Random();         int bucky[] = new int[7];              //yaha ek cheez note karo for loop mein jo counter int use kiya hai uska use nhi kiya hai andar yaani yaha bas itna hi ki loop  ko kitni baar chalana ha         for(int counter =0; counter<1000;counter++) {             ++bucky[1+rand.nextInt(6)];  // yaha ++ ka matlab hai ki bucky[x] ki value 1 se add kardo         }                 for(int i=1;i<bucky.length;i++) {             System.out.println(bucky[i]);         }     } } OUTPUT

Printing a 2D Array

Image
 public class orange {     public static void main (String args[]) {     int buckyArray[][] = {{1,4,5,9} , {4,11,65,85,12,75}, {96,63,25}};          for(int row = 0 ; row<buckyArray.length; row++) {         for(int column =0 ; column<buckyArray[row].length; column++) {             System.out.print(buckyArray[row][column] + "\t");         }                  System.out.println();     } } } OUTPUT

Question Practice on Random Number and Loops

 Q.Write a program that generates a random number and asks the user to guess what the number is. If the user's guess is higher than the random number, the program should display "Too high, try again." If the user's guess is lower than the random number, the program should display "Too low, try again." The program should use a loop that repeats until the user correctly guesses the random number. SOLUTION import java.util.Random; import java.util.Scanner; public class orange {     public static void main(String[] args) {         Random bucky = new Random();         Scanner bucky1 = new Scanner(System.in);         int number, guess;                 number = bucky.nextInt(6);         System.out.println("Your Guess?");         guess = bucky1.nextInt();                 while (number != guess) {         if (number < guess) {             System.out.println("Too High, Try Again");             guess = bucky1.nextInt();