Java - Introduction to Programming
Lecture 1
Installation & First Program
1. Install Java
a. Install JDK (https://www.oracle.com/in/java/technologies/javase-
downloads.html)
b. Install IntelliJ
(https://www.jetbrains.com/idea/download/#section=mac)
OR
b. Install Visual Studio Code (VS Code) - Prefer THIS
(https://code.visualstudio.com/download)
2. 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.
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.
3. Our 1st Program
public class Main {
public static void main(String[] args) {
// Our 1st Program
System.out.println("Hello World");
}
}
Java - Introduction to Programming
Lecture 2
Variables & Data Types
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;
}
}
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
Primitive Data Types
These are the data types of fixed size.
Data Type Meaning Size Range
(in Bytes)
byte2’s complement integer 1 -128 to 127
short2’s complement integer 2 -32K to 32K
int Integer numbers 4 -2B to 2B
long2’s complement integer 8 -9,223,372,036,854,775,808
to 9,223,372,036,854,775,807
(larger values)
float Floating-point 4 Upto 7 decimal digits
double Double Floating-point 8 Upto 16 decimal digits
char Character 2 a, b, c ..
A, B, C ..
@, #, $ ..
bool Boolean 1 True, false
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. 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;
}
}
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.
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.
3. Make a program that prints the table of a number
that is input by the user.
(HINT - You will have to write 10 lines for this but as
we proceed in the course you will be studying about
‘LOOPS’ that will simplify your work A LOT!)
KEEP LEARNING & KEEP PRACTICING :)
Java - Introduction to Programming
Lecture 3
1. Conditional Statements ‘if-else’
The if block is used to specify the code to be executed if the
condition specified in if is true, the else block is executed
otherwise.
int age = 30;
if(age > 18) {
System.out.println("This is an adult");
} else {
System.out.println("This is not an adult");
}
2. Conditional Statements ‘switch’
Switch case statements are a substitute for long if statements that
compare a
variable to multiple values. After a match is found, it executes the
corresponding code of that value case.
The following example is to print days of the week:
int n = 1;
switch(n) {
case 1 :
System.out.println("Monday");
break;
case 2 :
System.out.println("Tuesday");
break;
case 3 :
System.out.println("Wednesday");
break;
case 4 :
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6 :
System.out.println("Saturday");
break;
default :
System.out.println("Sunday");
}
Homework Problems
1. 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.
2. 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.
KEEP LEARNING & KEEP PRACTICING :)
Java - Introduction to Programming
Lecture 4
Loops
A loop is used for executing a block of statements repeatedly until
a particular condition is satisfied. A loop consists of an
initialization statement, a test condition and an increment
statement.
For Loop
The syntax of the for loop is :
for (initialization; condition; update) {
// body of-loop
}
for (int i=1; i<=20; i++) {
System.out.println(i);
}
While Loop
The syntax for while loop is :
while(condition) {
// body of the loop
}
int i = 0;
while(i<=20) {
System.out.println(i);
i++;
}
Do-While Loop
The syntax for the do-while loop is :
do {
// body of loop;
}
while (condition);
int i = 0;
do {
System.out.println(i);
i++;
} while(i<=20);
Homework Problems
1. Print all even numbers till n.
2. Run
for(; ;) {
System.out.println("Apna College");
}
loop on your system and analyze what happens. Try to think of the
reason for the output produced.
3. Make a menu driven program. The user can enter 2 numbers,
either 1 or 0.
If the user enters 1 then keep taking input from the user for a
student’s marks(out of 100).
If they enter 0 then stop.
If he/ she scores :
Marks >=90 -> print “This is Good”
89 >= Marks >= 60 -> print “This is also Good”
59 >= Marks >= 0 -> print “This is Good as well”
Because marks don’t matter but our effort does.
(Hint : use do-while loop but think & understand why)
BONUS
Qs. Print if a number is prime or not (Input n from the user).
[In this problem you will learn how to check if a number is prime or not]
Homework Solution (Lecture 3)
import java.util.*;
public class Conditions {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int operator = sc.nextInt();
/**
* 1 -> +
* 2 -> -
* 3 -> *
* 4 -> /
* 5 -> %
*/
switch(operator) {
case 1 : System.out.println(a+b);
break;
case 2 : System.out.println(a-b);
break;
case 3 : System.out.println(a*b);
break;
case 4 : if(b == 0) {
System.out.println("Invalid Division");
} else {
System.out.println(a/b);
}
break;
case 5 : if(b == 0) {
System.out.println("Invalid Division");
} else {
System.out.println(a%b);
}
break;
default : System.out.println("Invalid Operator");
}
}
}
Java - Introduction to Programming
Lecture 5
Patterns - Part 1
1.
import java.util.*;
public class Patterns {
public static void main(String args[]) {
int n = 5;
int m = 4;
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
2.
import java.util.*;
public class Patterns {
public static void main(String args[]) {
int n = 5;
int m = 4;
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
if(i == 0 || i == n-1 || j == 0 || j == m-1) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
3.
import java.util.*;
public class Patterns {
public static void main(String args[]) {
int n = 4;
for(int i=1; i<=n; i++) {
for(int j=1; j<=i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
4.
import java.util.*;
public class Patterns {
public static void main(String args[]) {
int n = 4;
for(int i=n; i>=1; i--) {
for(int j=1; j<=i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
5.
import java.util.*;
public class Patterns {
public static void main(String args[]) {
int n = 4;
for(int i=n; i>=1; i--) {
for(int j=1; j<i; j++) {
System.out.print(" ");
}
for(int j=0; j<=n-i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
6.
import java.util.*;
public class Patterns {
public static void main(String args[]) {
int n = 5;
for(int i=1; i<=n; i++) {
for(int j=1; j<=i; j++) {
System.out.print(j);
}
System.out.println();
}
}
}
7.
import java.util.*;
public class Patterns {
public static void main(String args[]) {
int n = 5;
for(int i=n; i>=1; i--) {
for(int j=1; j<=i; j++) {
System.out.print(j);
}
System.out.println();
}
}
}
8.
import java.util.*;
public class Patterns {
public static void main(String args[]) {
int n = 5;
int number = 1;
for(int i=1; i<=n; i++) {
for(int j=1; j<=i; j++) {
System.out.print(number+" ");
number++;
}
System.out.println();
}
}
}
9.
import java.util.*;
public class Patterns {
public static void main(String args[]) {
int n = 5;
for(int i=1; i<=n; i++) {
for(int j=1; j<=i; j++) {
if((i+j) % 2 == 0) {
System.out.print(1+" ");
} else {
System.out.print(0+" ");
}
}
System.out.println();
}
}
}
Homework Problems (Solutions in next Lecture’s
Video)
1. Print a solid rhombus.
2. Print a number pyramid.
3. Print a palindromic number pyramid.
Homework Solution (Lecture 4)
1. Print all even numbers till n.
1. public class Solutions {
2. public static void main(String args[]) {
3. int n = 25;
4.
5. for(int i=1; i<=n; i++) {
6. if(i % 2 == 0) {
7. System.out.println(i);
8. }
9. }
10. }
11. }
12.
3. Make a menu driven program. The user can enter 2 numbers, either
1 or 0.
If the user enters 1 then keep taking input from the user for a
student’s marks(out of 100).
If they enter 0 then stop.
If he/ she scores :
Marks >=90 -> print “This is Good”
89 >= Marks >= 60 -> print “This is also Good”
59 >= Marks >= 0 -> print “This is Good as well”
Because marks don’t matter but our effort does.
(Hint : use do-while loop but think & understand why)
import java.util.*;
public class Solutions {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int input;
do {
int marks = sc.nextInt();
if(marks >= 90 && marks <= 100) {
System.out.println("This is Good");
} else if(marks >= 60 && marks <= 89) {
System.out.println("This is also Good");
} else if(marks >= 0 && marks <= 59) {
System.out.println("This is Good as well");
} else {
System.out.println("Invalid");
}
System.out.println("Want to continue ? (yes(1) or no(0))");
input = sc.nextInt();
} while(input == 1);
}
}
Qs. Print if a number n is prime or not (Input n from the user).
[In this problem you will learn how to check if a number is prime or not]
import java.util.*;
public class Solutions {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
boolean isPrime = true;
for(int i=2; i<=n/2; i++) {
if(n % i == 0) {
isPrime = false;
break;
}
}
if(isPrime) {
if(n == 1) {
System.out.println("This is neither prime not composite");
} else {
System.out.println("This is a prime number");
}
} else {
System.out.println("This is not a prime number");
}
}
}
Java - Introduction to Programming
Lecture 6
Patterns - Part 2
1.
import java.util.*;
public class Solutions {
public static void main(String args[]) {
int n = 4;
//upper part
for(int i=1; i<=n; i++) {
for(int j=1; j<=i; j++) {
System.out.print("*");
}
int spaces = 2 * (n-i);
for(int j=1; j<=spaces; j++) {
System.out.print(" ");
}
for(int j=1; j<=i; j++) {
System.out.print("*");
}
System.out.println();
}
//lower part
for(int i=n; i>=1; i--) {
for(int j=1; j<=i; j++) {
System.out.print("*");
}
int spaces = 2 * (n-i);
for(int j=1; j<=spaces; j++) {
System.out.print(" ");
}
for(int j=1; j<=i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
2.
import java.util.*;
public class Solutions {
public static void main(String args[]) {
int n = 5;
for(int i=1; i<=n; i++) {
//spaces
for(int j=1; j<=n-i; j++) {
System.out.print(" ");
}
//stars
for(int j=1; j<=n; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
3.
import java.util.*;
public class Solutions {
public static void main(String args[]) {
int n = 5;
for(int i=1; i<=n; i++) {
//spaces
for(int j=1; j<=n-i; j++) {
System.out.print(" ");
}
//numbers
for(int j=1; j<=i; j++) {
System.out.print(i+" ");
}
System.out.println();
}
}
}
4.
import java.util.*;
public class Solutions {
public static void main(String args[]) {
int n = 5;
for(int i=1; i<=n; i++) {
//spaces
for(int j=1; j<=n-i; j++) {
System.out.print(" ");
}
//first part
for(int j=i; j>=1; j--) {
System.out.print(j);
}
//second part
for(int j=2; j<=i; j++) {
System.out.print(j);
}
System.out.println();
}
}
}
5.
import java.util.*;
public class Solutions {
public static void main(String args[]) {
int n = 5;
//upper part
for(int i=1; i<=n; i++) {
//spaces
for(int j=1; j<=n-i; j++) {
System.out.print(" ");
}
for(int j=1; j<=2*i-1; j++) {
System.out.print("*");
}
System.out.println();
}
//lower part
for(int i=n; i>=1; i--) {
//spaces
for(int j=1; j<=n-i; j++) {
System.out.print(" ");
}
for(int j=1; j<=2*i-1; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
Homework Problems
1. Print a hollow Butterfly.
2. Print a hollow Rhombus.
*****
* *
* *
* *
*****
3. Print Pascal’s Triangle.
11
121
1331
14641
4. Print half Pyramid.
12
123
1234
12345
5. Print Inverted Half Pyramid.
11111
222
33