Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

basics of java with examples, Summaries of Java Programming

basics and syntax of java,examples,programs

Typology: Summaries

2017/2018

Uploaded on 09/11/2022

neha-yarrapothu
neha-yarrapothu 🇮🇳

5 documents

1 / 67

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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");
}
}
Apna College
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43

Partial preview of the text

Download basics of java with examples and more Summaries Java Programming in PDF only on Docsity!

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.

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.

  1. 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.
  2. 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");

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]

Java - Introduction to Programming

Lecture 5

Patterns - Part 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(); } } }

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

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

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.