Java Basics
Print Output
OUTPUT
[Link](“Hello”); Helloworld
[Link](“world”);
[Link](“Hello”); Hello
[Link](“World”); World
Input
Import [Link];
Scanner sc = new Scanner([Link]); // create Scanner variable
int number = [Link](); // read integer
Import [Link];
public class Test{
public static void main(String[] arg){
Scanner sc = new Scanner([Link]);
int number = [Link]();
[Link](“Number is “+number);
}
}
Conditional statements
if… else if
if… else •Syntax
•Syntax if(condition1){
// statements
if(condition){
}
// statements
else if(condition2){
}
// statements
else{
}
// statements
else{
}
// statements
}
Conditional statements
switch …. case
switch (expression) { // Sample code
case value1: int x = 1;
// statements
break; switch (x) {
case 1:
case value2: [Link]("One");
// statements break;
break; case 2:
[Link]("Two");
case value3: break;
// statements default:
break; [Link]("Done");
}
default:
// statements
OUTPUT
} One
for-each loop
Loop constructs for (datatype variable : collection) {
// loop body
for loop }
for (initialization; condition; update) {
// loop body
}
int[] nums = {10, 20, 30, 40, 50};
for (int i = 1; i <= 5; i++) { for (int n : nums) {
[Link](i); [Link](n);
} }
Print hello n times
import [Link];
OUTPUT
public class Main{
Enter n : 5
public static void main(String[] args) {
Scanner sc = new Scanner([Link]); 1 Hello
[Link]("Enter n : "); 2 Hello
int n = [Link](); 3 Hello
for(int i =1; i<=n; i++){ 4 Hello
[Link](i+" Hello");
5 Hello
}
}
}
Print Even or Odd
public class EvenOddExample {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
[Link](i + " is Even");
} else {
[Link](i + " is Odd");
}
}
}
}
basics
Data types: Input:
int num; int - nextInt()
char ch=' '; char – next().charAt(0)
float fNum; float - nextFloat()
double dNum; double - nextDouble()
String str; String - next() or nextLine()
boolean flag; boolean - nextBoolean()
Output Formatting:
[Link](“%d x %d = %d“, a,b, a*b);
Output Formatting
int age = 21;
String name = "Amrita";
[Link]("%s , your age is %d", name, age);
Amrita , your age is 21 Specifier Type
%s String
%d Integer
%f Floating point
%c Character
%b Boolean
%n New line
Practice Questions - loop
1. Write a Java program to display the first 10 natural numbers.
2. Write a program in Java to input 5 numbers from the keyboard
and find their sum and average.
3. Write a Java program to display the cube of the given number up
to an integer.
Test Data
Input number of terms : 4
Expected Output :
Number is : 1 and cube of 1 is : 1
Number is : 2 and cube of 2 is : 8
Number is : 3 and cube of 3 is : 27
Number is : 4 and cube of 4 is : 64
Practice Questions
4. Write a Java program that displays the sum of n odd natural numbers.
5. Write a Java program to display the pattern like a right-angled triangle with a
number.
Test Data
Input number of rows : 10
Expected Output :
1
12
123
1234
12345
123456
1234567
12345678
123456789
12345678910
Function
access_modifier return_type functionName(parameters) {
// function body
return value; // (if return_type is not void)
}
public static void greet() {
[Link]("Welcome to Java Programming!");
}
Define and call a function
public class Main{
public static void main(String[] args) {
greet(); // function call
}
public static void greet(){ //function definition
[Link]("Greetings");
}
}
Define a function to add 2 integers
public class Main{
public static void main(String[] args) {
int num1=10;
int num2 = 20;
int total = sum(num1, num2);
[Link]("Total = "+total);
}
public static int sum(int n1, int n2){
return n1+n2;
}
}
Practice Questions - function
1. Write a Java method to find the smallest number among three
numbers.
2. Write a Java method to compute the average of three numbers.
3. Write a Java method to compute the sum of digits in an integer.
4. Write a Java method that accepts three integers and checks
whether they are consecutive or not. Returns true or false.
QUIZ
Q1
Q2
Q3
Q4
Q5
Q6
Q7
Q8
Q9