Program Concepts. Lecture by DR. Juniorika Lyngdoh.
import iAYA,uULsganngc; public class Expl public static void main(String[] angs) Scanner = new Scanner(System.in); first number: double a = sgzneÜQeune(); second number: double b = sgængÜQgubLe(); 't); V'); double sum = a + b double diff double prod double div = a / b b; ( " Sum " + sum); "Difference = " + diff); + prod); "Division = " + div);.
import java.util.Scanner; This line imports the Scanner class from the java.util package , which allows the program to take input from the keyboard. public class Exp1 This defines a public class named Exp1.Every Java program must begin class and must have a class, and all code is written inside a class. public static void main(String[] args) This is the main method, where the program starts executing. Every java program starts executing from this method.
public Accessible from any where.JVM can call main() from outside the class. Without public, the program will not run because JVM cannot access the method. static The main() method belongs to the class, not to an object. This means Java can run main() without creating an object of the class. JVM calls main() directly using the class name..
void Returns nothing. The main() method does not give any output back to the JVM. It just runs our program. main() Starting point of every Java program. When we run a Java program, JVM looks for this main as the entry point..
String args[] Array of Strings. Square brackets [] means It stores multiple strings like args[0], args[1], … args is just a variable name for the argument list. It represents an array of Strings that stores command-line arguments. We can rename it to anything, like val[], data[], myArgs[], etc. java Exp1 Hello World Example: If we run: then: args[0] = "Hello" args[1] = "World".
Scanner sc = new Scanner(System.in); Creates a Scanner object named sc. System.in means the scanner will read input from the keyboard. System.out.print("Enter first number: "); It displays the message double a = sc.nextDouble(); Reads a double value typed by the user and stores it in variable a..
double b = sc.nextDouble(); Reads the second double value and stores it in variable b. double sum = a + b; Calculates addition of a and b and stores the result in variable sum. double diff = a - b; Calculates subtraction (a minus b) and stores result in diff. System.out.println("Sum = " + sum); Prints the message Sum = followed by the calculated sum..
System System is a built-in class in Java. It belongs to the java.lang package.It contains useful tools like in, out, err. out out is a static object inside the System class.It represents the standard output, usually the computer screen.Where the output will be displayed. println println() is a method that prints a message and then goes to a new line..
"Sum = " This is a string literal written inside double quotes. + This is the string concatenation operator. It joins/combine strings and variables. sum This is a variable (a double in your program). It contains the computed sum of the numbers.
Square of a number using command line. public class Exp2 public static void main(String[] a.cg.s) int num = int square=num*num; + square);.
import jaym.utiX.sganne-c; public class Exp3 public static void main(String[] a-cgs) scanner = new scanner (System. in); how many int n ; number 1: int largest = ss.e-uextxnt(); for (int i = ( " Enter number int x - scxuexun.t(); if (x > largest) largest = x; number - " numbers. first number is largest for now largest) ;.