Java Basics (Notes
Java Basics (Notes
1. Sample Code
Functions
A function is a block of code which takes some input, performs some operations and
returns some output.
The functions stored inside classes are called methods.
The function we have used is called main (return type: void).
Class
A class is a group of objects which have common properties. A class can have some
properties and functions (called methods).
The class we have used is Main.
(b) Execution: JVM converts byte code into native code ( code machine can
understand), now any sytem can understand this code.
___________________________________________________________________________________
__________________________________________________
3.1 Variables
A variable is a container (storage area) used to hold data.
Each variable should be given a unique name (identifier).
package com.apnacollege;
Eg : String, Arrays
3.3 Constants
A constant is a variable in Java which has a fixed value i.e. it cannot be assigned
a different value once assigned.
package com.apnacollege;
- "System" is a class.
- "print" is a function where cursor remains in the same line same place.
- "println" is also a function where cursor moves to the next line.
- "\n" also moves the cursor to the next line.
- every command in java ends with semi colon ; ,
- single quotes can be used instead of double quotes. (but double quotes is
preferred in java)
3.5 Input in Java
import java.util.*;
Question: Take two variables a and b from the user and print their sum.
Answer: import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int a= sc.nextInt();
int b= sc.nextInt():
int sum= a+b;
System.out.print("Sum of the two no. is:", sum);
Homework Problems
1. Try to declare meaningful variables of each type. Eg - a variable named age
should be a numeric type (int or float) not byte.
Answer:
int height = 126;
bool answer = "true";
float price = 25.56;
2. Make a program that takes the radius of a circle as input, calculates its radius
and area and prints it as output to the user.
import java.util.*;
public class Main {
public static void main(String[]args) {
Scanner sc= new Scanner(System.in);
int rad= sc.nextInt;
int area = 3.14*rad*rad;
System.out.println("Area of the circle is:", area);
3. Make a program that prints the table of a number that is input by the user.
import java.util.*;
public class Main {
public static void main(String[]args) {
Scanner sc= new Scanner(System.in);
int num=sc.nextInt;
int one= num*1;
int two= num*2;
int three= num*3;
int four= num*4;
int five=num*5;
int six=num*6;
int seven=num*7;
int eight=num*8;
int nine=num*9;
int ten=num*10;
System.out.println("The table of this number is:", one);
System.out.println(two);
System.out.println(three);
......
___________________________________________________________________________________
__________________________________________________
Q) Ask the user to enter the number of the month & print the name of the month. For
eg - For ‘1’ print ‘January’, ‘2’ print ‘February’ & so on.
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a month's no.");
int num= sc.nextInt();
switch(num) {
case 1: System.out.println("January");
break;
case 2: System.out.println("February");
break;
case 3: System.out.println("March");
break;
case 4: System.out.println("April");
break;
case 5: System.out.println("May");
break;
case 6: System.out.println("June");
break;
case 7: System.out.println("July");
break;
case 8: System.out.println("August");
break;
case 9: System.out.println("September");
break;
case 10: System.out.println("October");
break;
case 11: System.out.println("November");
break;
case 12: System.out.println("December");
break;
default: System.out.println("Invalid");
}
}
}
Q) Make a Calculator. Take 2 numbers (a & b) from the user and an operation as
follows :
1 : + (Addition) a + b
2 : - (Subtraction) a - b
3 : * (Multiplication) a * b
4 : / (Division) a / b
5 : % (Modulo or remainder) a % b
Calculate the result according to the operation given and display it to the user.
import java.sql.SQLOutput;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number");
int a= sc.nextInt();
System.out.println("Enter second number");
int b= sc.nextInt();
System.out.println("Enter an operator");
char op= sc.next().charAt(0);
switch (op) {
case '+': int add= a+b;
System.out.println(add);
break;
case '-': int sub= a-b;
System.out.println(sub);
break;
case '*': int mul= a*b;
System.out.println(mul);
break;
case '/': int div= a/b;
System.out.println(div);
break;
case '%': int mod= a%b;
System.out.println(mod);
break;
default:
System.out.println("Invalid Operator");
}
}
}
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.println("Enter a no.");
int n= sc.nextInt();
for(int i=2; i <= n; i= i+2 ) {
System.out.println(i);
}