Java Introduction
Java Introduction
___________________________________________________________________________________
_____________________________________
🧊 Variables X and Y are initialized with the string values "water" and "kool-aid"
respectively.
🔄 Directly assigning X to Y or Y to X doesn't work for swapping their values.
🔄 To swap variables X and Y, create a temporary variable (temp), store one
variable's value in temp, then assign the other variable's value to the first
variable and temp's value to the second variable.
🤝 Swapping variables manually with a temporary variable (temp) is a common
technique when a programming language doesn't provide a direct swap function.
CODE:
String x = "water";
String y = "Kool-Aid";
String temp;
temp = x;
x=y;
y=temp;
System.out.println("x: "+x);
System.out.println("y: "+y);
}
}
___________________________________________________________________________________
___________________________________
HOW TO TAKE INPUT IN JAVA:
️️ Import the Scanner class from the java.util package to use it for user input.
📝 Create a Scanner object and use it to prompt and accept user input for strings
and integers.
❌ Be cautious about input data types to avoid input mismatches.
🔄 Use nextLine() method to clear the Scanner after reading other data types.
___________________________________________________________________________________
___________________________________
️️ To accept user input in Java, you need to use the `Scanner` class, which is part
of the `java.util` package.
🤖 Create a `Scanner` object to accept user input: `Scanner scanner = new
Scanner(System.in);`
🔄 Use `scanner.nextLine()` to accept a line of text as user input and store it in a
variable.
🔄 Ensure that the data type of the input matches what you expect (e.g., string for
text, integer for numbers).
🤖 Youcan accept different types of input, such as strings and integers, using the
appropriate methods (`nextLine()` and `nextInt()`).
🔄 When using `nextLine()` after `nextInt()`, consider adding an extra
`scanner.nextLine()` to clear any remaining newline characters in the input buffer.
📊 You can create a basic graphical user interface (GUI) application in Java using
`JOptionPane` to display dialog boxes.
📝 Import `javax.swing.JOptionPane` to work with `JOptionPane` class.
📥 Use `JOptionPane.showInputDialog` to create an input dialog box, and store the
result as a string.
📤 Use `JOptionPane.showMessageDialog` to display a message dialog box.
🧮 When using `JOptionPane.showInputDialog`, remember to convert the result to the
appropriate data type (e.g., `Integer.parseInt` for integers).
🌟 You can work with different data types, such as doubles, by parsing the input
appropriately.
🚀 This basic GUI is just an introduction; more advanced GUI topics will be covered
later in the playlist.
Code:
import javax.swing.JOptionPane;
public class FirstClass {
import java.util.*;
public class FirstClass {
public static void main(String [] args) {
Scanner sc= new Scanner(System.in);
System.out.println("Enter values of base and perpendicular");
double a= sc.nextDouble();
double b= sc.nextDouble();
double c= Math.sqrt((a*a)+(b*b));
System.out.println("The value of hypotenuse is :"+ c);
sc.close();
}
}
___________________________________________________________________________________
___________________________________
JAVA RANDOM NUMBERS: