0% found this document useful (0 votes)
62 views6 pages

Java Basics (Notes

This document contains notes on Java programming concepts like functions, classes, variables, data types, input/output etc. It provides sample code to demonstrate these concepts and includes questions and answers for homework problems involving basic programming tasks like calculating area of a circle, printing tables, building a calculator etc. The document is intended to teach Java programming fundamentals to beginners.

Uploaded by

Shaziaa Anwer
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
0% found this document useful (0 votes)
62 views6 pages

Java Basics (Notes

This document contains notes on Java programming concepts like functions, classes, variables, data types, input/output etc. It provides sample code to demonstrate these concepts and includes questions and answers for homework problems involving basic programming tasks like calculating area of a circle, printing tables, building a calculator etc. The document is intended to teach Java programming fundamentals to beginners.

Uploaded by

Shaziaa Anwer
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1/ 6

JAVA 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.

Our 1st Program

public class Main {

public static void main(String[] args) {


// Our 1st Program
System.out.println("Hello World");
}
}

Question: How is my code running?


Answer: There are two stages:

(a) Compilation: There is a component known as JRE ( Java Runtime Environment) in


JDK (Java Development Kit) that we installed. There is one more component inside of
JRE known as JVM (Java virtual Machine). Our source code(extension: .java) goes to
the compiler in JDK and this compiler converts this source code into byte code
(extension: .class). This byte code can run on any operating system as long as that
system contains JRE. You cannot do this on C++, that's why Java is called a
portable language, You can run apps made by java on any operating system.

(b) Execution: JVM converts byte code into native code ( code machine can
understand), now any sytem can understand this code.
___________________________________________________________________________________
__________________________________________________

3. Variables & Data Types

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;

public class Main {

public static void main(String[] args) {


// Variables
String name = "Aman";
int age = 30;

String neighbour = "Akku";


String friend = neighbour;
}
}

3.2 Data Types


Data types are declarations for variables. This determines the type and size of
data associated with variables which is essential to know since different data
types occupy different sizes of memory.

There are 2 types of Data Types :


Primitive Data types : to store simple values
Non-Primitive Data types : to store complex values

3.2.1 Primitive Data Types


These are the data types of fixed size.

Eg: byte(1), short(2), int(4), char(2), boolean(1), long(8), float(4),


double(8). (size is in bytes)

3.2.2 Non-Primitive Data Types


These are of variable size & are usually declared with a ‘new’ keyword.

Eg : String, Arrays

String name = new String("Aman");


int[] marks = new int[3];
marks[0] = 97;
marks[1] = 98;
marks[2] = 95;

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;

public class Main {

public static void main(String[] args) {


// Constants
final float PI = 3.14F;
}
}

3.4 Output in Java

System.out. print("Hello World");


System.out.println("Hello World");
System.out.print("Hello World \n");

- "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.*;

public class Main {


public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
String name = sc.next(); //input name
System.out.print(name);

- sc.next only accepts only one token.


- sc.nextLine accepts n no. of token.
- sc.nextInt inputs integer data type.
- sc.nextFloat inputs float data type.

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");

}
}
}

Q) Print all even numbers till n.

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);
}

You might also like