Posts

Day15: Loop

Image
 Loop(Iterative Statement) In Java loops are two types: Entry Controlled Loop(Condition is tested before the body Ex:- while, for) Exit Controlled Loop(Condition is tested after loop body Ex:- do while) Syntax for while loop: while(test-condition)  {     //loop body } class PrintNos { public static void main(String[] args)      {     int i = x;     while(i<=10);          {               System.out.println(i);               i++;          }      } } Output : 1 2 3 4 5 6 7 8 9 10 Factorial by while loop import java.util.Scanner; class Factorial { public static void main(String [] args) { Scanner sc = new Scanner(System.in); int n,f = 1; System.out.println("Enter an int: "); n = sc.nextInt(); while(n>1) { f = f * n; n--; } System.out.println("factorial ...

Day14: Using Scanner Class

Image
 Using Scanner Class We can use scanner class to take an input by the user. It is the second method while we take an input from the user.  System.out.println() System.in.read() ---> read will only accept char and show its unicode. Here "in" is object reference(taking input from keyboard) and "read" is method. Scanner Method : int---> int nextInt() float ---> float nextFloat double--->double nextDouble String ----> next() Byte ---> nextByte() Long ----> nextLong() boolean ---> nextBoolean() Scanner S = new Scanner(System.in);   //Write a program to Add two number taking by user with the help of Scanner.  import java.util.Scanner; class AddNo { public static void main(String [ ] args) { //Create a Scanner object and connect it with kb Scanner kb = new Scanner(System.in); int a, b, c; System.out.println("Enter First Number "); a = kb.nextInt(); System.out.println("Enter Second Number "); b = kb.nextIn...

Day13 : Ternary Operator

Syntax :   Var-name = (test-condition) ? (true - statement) : (false - statement); C : (a%2==0)?printf ("Even no") : printf("Odd no"); Java : (Wrong way) (a%2==0) ? System.out.println("Even No") : System.out.print("Odd No.); Java : (Correct Way) class Evenodd {     public static void main(String[] args)     {          int a;          a = integer.parseInt(array[0]);          String result;          result = (a%2 == 0) ? "Even" : "Odd";          System.out.println(result);     } }

Day12: Switch Statement

 Switch Statement Swich(var - name) //byte, short, int, char, string {     Case Value:                    Statement                    Break;     Case Value:                    Statement                    Break;     default:                    Statement }

Day11: Using "String" Class

Image
 Using String Class In java "String" is used one of the most commonly used class and it provides a bunch of useful method for performing various operation. But most of the string class method are non-static by nature so to call these method create class of the object . class Student {       int roll;      char grade;      float percentage;     public static void main(String [] args)     {           Student S = new Student();       }        // OR     //Student S;     //Object Reference     //S = new Student(); }      Output: When we Run the above program : class Test {     public static void main(String[] args)     {          String S1;          S1 = new String("Bhopal");          String S2; ...

Day10 : Wrapper Class

Image
 Wrapper Class In java corresponding to 8 primitive datatype their is a set of 8 special classes. These classes have their names resembling with the names of the datatype and the database and collectively all 8 of them are called as wrapper classes. All these wrapper class are available in the package "java.lang" and following are their names. Integer Byte Short Long Float Double Character Boolean What is the use of wrapper class in java? Wrapper classes are use by programmer for two purposes : (1) We use them for converting string representation of the value to its original primitive form. For Example : To convert "25" to 25. We use the parseInt() belonging to wrapper class integer as shown below: int a; a = Integer.parseInt("25"); Likewise we've other parseXXX() method belonging to their respective wrapper classes. (2) Programmer also use wrapper class for converting a variable of primitive datatype into corresponding object. For Example :           ...

Day9 : Accepting Input

Image
 Accepting Input We can take input by the user by three method :  Using Command line Argument Using Scanner class Using GUI control 1) Using Command Line Argument When we are giving an argument at the time of execution after compiling a program in command prompt then we use command line argument . class Test { public static void main(String[] args) { System.out.println("Hello "+args[0]); System.out.println("Thank You"); } } How to Run: Open the notepad with Run as Administrator. Copy Paste the above code Save in bin file(if path is not specified) Open command prompt with Run as administrator. Enter the file location with cd command like as cd C:\Program Files\Java\jdk-17.0.4.1\bin then press Enter button Then write command javac File_name.java(Example javac Test.java) then press Enter button. Now our program is easily compiled. We're ready to run the above code with this command java Test any_String (Example java Test SCA_Notes) and then press the En...