You are on page 1of 35

List of Programs :

1 Develop an in depth understanding of programming in Java: data types,variables,


operators, operator precedence, Decision and control statements, arrays,switch
statement, Iteration Statements, Jump Statements, Using break, Using continue,
return.
2 Write Object Oriented programs in Java: Objects, Classes constructors, returning
and passing objects as parameter, Inheritance, Access Control, Using super,
finalwith inheritance Overloading and overriding methods, Abstract classes,
Extended classes.
3 Develop understanding to developing packages & Interfaces in Java: Package,
concept of CLASSPATH, access modifiers, importing package, Defining
andimplementing interfaces.

4 Develop understanding to developing Strings and exception handling: String


constructors, special string operations, character extraction, searching
andcomparing strings, string Buffer class. Exception handling fundamentals,
Exception types, uncaught exceptions, try, catch and multiple catch
statements.Usage of throw, throws and finally.
5 Develop applications involving file handling: I/O streams, File I/O.
Program 1
Develop an in depth understanding of programming in
Java: data types,variables, operators, operator precedence,
Decision and control statements, arrays,switch statement,
Iteration Statements, Jump Statements, Using break,
Using continue, return.

DATA TYPES
class JavaExample {
public static void main(String[] args) {

byte num;

num = 113;
System.out.println(num);
}
}

Output:
113

VARIABLES
class Simple{
public static void main(String[] args){
int a=10;
int b=10;
int c=a+b;
System.out.println(c);
}}
Output:
20

OPERATORS
class OperatorExample{
public static void main(String args[]){
int x=10;
System.out.println(x++);//10 (11)
System.out.println(++x);//12
System.out.println(x--);//12 (11)
System.out.println(--x);//10
}}
Output:
10
12
12
10

OPERATOR PRECEDENCE
class Precedence {
public static void main(String[] args) {
int a = 10, b = 5, c = 1, result;
result = a-++c-++b;

System.out.println(result);
}
}
Output:
2

ARRAYS
class Testarray{
public static void main(String args[]){
int a[]=new int[5];//declaration and instantiation
a[0]=10;//initialization
a[1]=20;
a[2]=70;
a[3]=40;
a[4]=50;
//traversing array
for(int i=0;i<a.length;i++)//length is the property of array
System.out.println(a[i]);
}}

Output:
10
20
70
40
50

SWITCH STATEMENT
public class SwitchCaseExample1 {

public static void main(String args[]){


int num=2;
switch(num+2)
{
case 1:
System.out.println("Case1: Value is: "+num);
case 2:
System.out.println("Case2: Value is: "+num);
case 3:
System.out.println("Case3: Value is: "+num);
default:
System.out.println("Default: Value is: "+num);
}
}
}
Output:
Default: Value is: 2

ITERATION STATEMENTS
public class ForExample {
public static void main(String[] args) {
//Code of Java for loop
for(int i=1;i<=10;i++){
System.out.println(i);
}
}
}

Output:
1
2
3
4
5
6
7
8
9
10

JUMP STATEMENTS
class Test
{
public static void main(String[] args)
{
for(int j=1; j<=100; j++)
{
if(j%2==0)
{
continue;
}
System.out.println(j);
}
}
}

Output:
1
3
5
.
.
99

USING BREAK
class Test
{
public static void main(String[] args)
{
for(int j=0; j<10; j++)
{
if(j==5)
{
break;
}
System.out.println(j);
}
System.out.println("outside of for loop");
}
}

Output:
0
1
2
3
4
outside of for loop

USING CONTINUE
class Test
{
public static void main(String[] args)
{
out:
for(int i=1; i<=100; i++)
{
System.out.println("outer");
for(int j=1; j<=100; j++)
{
System.out.println("nested");
if(j==2)
{
// continue; this will skip second(j==2) iteration of inner for loop
only
continue out; // this will skip current iteration of both for loops
}
}
}
}
}

Output:
The outer for loop will iterate 100 times but the inner for loop will
iterate twice each time.

outer
nested
nested
outer
nested
nested
.
.
.
outer
nested
nested
RETURN
class Test
{
public static void main(String[] args)
{
Test t = new Test();
int sum = t.addition(10,20); //addition() method return integer value
System.out.println("Sum = "+sum);
t.show("Devavrat"); //show() method does not return any value
}
int addition(int a,int b)
{
return a+b;
}
void show(String name)
{
System.out.println("Welcome "+name);
return; // not returning anything, it is optional
}
}

Output:

Sum = 30
Welcome Devavrat
Program 2
Write Object Oriented programs in Java: Objects, Classes
constructors, returning and passing objects as parameter,
Inheritance, Access Control, Using super, finalwith
inheritance Overloading and overriding methods, Abstract
classes, Extended classes.

Objects
class Lamp {
boolean isOn;
void turnOn() {
isOn = true;
System.out.println("Light on? " + isOn);
}
void turnOff() {
isOn = false;
System.out.println("Light on? " + isOn);
}
}
class Main {
public static void main(String[] args) {
Lamp led = new Lamp();
Lamp halogen = new Lamp();
led.turnOn();
halogen.turnOff();
}
}

Output:
Light on? true
Light on? False

Classes Constructors
class Bike1
{
Bike1(){System.out.println("Bike is created");}
public static void main(String args[]){
Bike1 b=new Bike1();
}
}

Output:

Bike is created

Inheritance
class Animal {
String name;
public void eat() {
System.out.println("I can eat fruits");
}
}
class Dog extends Animal {
public void display() {
System.out.println("My name is " + name);
}
}

class Main {
public static void main(String[] args) {
Dog labrador = new Dog();
labrador.name = "Rohu";
labrador.display(); labrador.eat(); } }

Output:
My name is Rohu
I can eat fruits

Access Control
class Test {//from w ww.ja v a 2s . c om
int a; // default access
public int b; // public access
private int c; // private access
// methods to access c
void setc(int i) {
c = i;
}
int getc() {
return c;
}
}
public class Main {
public static void main(String args[]) {
Test ob = new Test();
ob.a = 1;
ob.b = 2;
ob.setc(100); // OK
System.out.println("a, b, and c: " + ob.a +
" " + ob.b + " " + ob.getc());
}
}

Output:
a,b, and c:1 2 100

Using Super
class Animal{
String color="white";
}
class Dog extends Animal{
String color="black";
void printColor(){
System.out.println(color);//prints color of Dog class
System.out.println(super.color);//prints color of Animal class
}
}
class TestSuper1{
public static void main(String args[]){
Dog d=new Dog();
d.printColor();
}}

Output:
black
white

Final With Inheritance


abstract class Shape
{
private double width;
private double height;
public Shape(double width, double height)
{ this.width = width;
this.height = height;
}
public final double getWidth()
{
return width;
}
public final double getHeight()
{
return height;
}
abstract double getArea();
}
class Rectangle extends Shape
{
public Rectangle(double width, double height)
{
super(width, height);
}
@Override
final double getArea()
{
return this.getHeight() * this.getWidth();
}

class Square extends Shape


{
public Square(double side)
{
super(side, side);
}
@Override
final double getArea()
{
return this.getHeight() * this.getWidth();
}
}

// Driver class
public class Test
{
public static void main(String[] args)
{
// creating Rectangle object
Shape s1 = new Rectangle(10, 20);
// creating Square object
Shape s2 = new Square(10);
// getting width and height of s1
System.out.println("width of s1 : "+ s1.getWidth());
System.out.println("height of s1 : "+ s1.getHeight());
// getting width and height of s2
System.out.println("width of s2 : "+ s2.getWidth());
System.out.println("height of s2 : "+ s2.getHeight());
//getting area of s1
System.out.println("area of s1 : "+ s1.getArea());
//getting area of s2
System.out.println("area of s2 : "+ s2.getArea());

}
}
Output:
width of s1 : 10.0
height of s1 : 20.0
width of s2 : 10.0
height of s2 : 10.0
area of s1 : 200.0
area of s2 : 100.0

Overloading And Overriding Methods


class Test {
public static void printArea(int x, int y) {
System.out.println(x * y);
}
public static void printArea(int x) {
System.out.println(x * x);
}
public static void printArea(int x, double y) {
System.out.println(x * y);
}
public static void printArea(double x) {
System.out.println(x * x);
}
public static void main(String[] args) {
printArea(2, 4);
printArea(2, 5.1);
printArea(10);
printArea(2.3);
}
}

Output:
8
10.2
100
5.289999999999999

Abstract Classes
abstract class Bike{
abstract void run();
}
class Honda4 extends Bike{
void run(){System.out.println("running safely");}
public static void main(String args[]){
Bike obj = new Honda4();
obj.run();
}
}

OUTPUT:
running safely
Program 3
Develop understanding to developing packages &
Interfaces in Java: Package, concept of CLASSPATH,
access modifiers, importing package, Defining
andimplementing interfaces.

Package
import static java.lang.System.*;
class StaticImportDemo
{
public static void main(String args[])
{
out.println("Hello");
}
}
Output:
Hello

Access Modifiers
class Data {
private String name;
// getter method
public String getName() {
return this.name;
}
// setter method
public void setName(String name) {
this.name= name;
}
}
public class Main {
public static void main(String[] main){
Data d = new Data();

// access the private variable using the getter and setter


d.setName("Programiz");
System.out.println(d.getName());
}
}
Output:
The name is Programiz

Interfaces
interface printable{
void print();
}
class A6 implements printable{
public void print(){System.out.println("Hello");}

public static void main(String args[]){


A6 obj = new A6();
obj.print();
}
}

Output:
Hello
Program 4
Develop understanding to developing Strings and
exception handling: String constructors, special string
operations, character extraction, searching andcomparing
strings, string Buffer class. Exception handling
fundamentals, Exception types, uncaught exceptions, try,
catch and multiple catch statements.Usage of throw,
throws and finally.
String Constructors
packagestringPrograms;
public class Science
{
public static void main(String[] args)
{
char chars[] = { 's', 'c', 'i', 'e', 'n', 'c', 'e' };
String s = new String(chars);
System.out.println(s);
}
}
Output:
Science

Special String Operations


public class ConcatExmaple {
public static void main(String[] args) {
String str = "javaguides";
str = str.concat(".net");
System.out.println("Concatenates the specified string to the end of
this string : " + str);

System.out.println("cares".concat("s"));
System.out.println("to".concat("get"));
}
}

Output:
Concatenates the specified string to the end of this string :
javaguides.net
caress
toget
Character Extraction
class temp
{
public static void main(String...s)
{
String str="Hello World";
char ch[]=new char[4];
str.getChars(1,5,ch,0);
System.out.println(ch);
}
}

Output:
Hello World

Searching And Comparing Strings


class Teststringcomparison1{
public static void main(String args[]){
String s1="Sachin";
String s2="Sachin";
String s3=new String("Sachin");
String s4="Saurav";
System.out.println(s1.equals(s2));//true
System.out.println(s1.equals(s3));//true
System.out.println(s1.equals(s4));//false
}
}

Output:
true
true
false

String Buffer Class


import java.io.*;
class GFG {
public static void main(String[] args)
{
StringBuffer s = new StringBuffer("GeeksforGeeks");
int p = s.length();
int q = s.capacity();
System.out.println("Length of string Right=" + p);
System.out.println("Capacity of string Left=" + q);
}
}

Output:
Length of string Right =13
Capacity of string Left =29

Exception Handling
public class ExceptionDemo {
public static void main (String[] args) {
int a=10;
for(int i=3;i>=0;i--)
try{
System.out.println(a/i);
}catch(ArithmeticException e){
System.out.println(e);
}
}
}
Output:
3
5
10

Uncaught Exceptions
class Random
implements Thread
.UncaughtExceptionHandler {
public void uncaughtException(
Thread t, Throwable e)
{
System.out.println(
"Welcome to my World");
}
}

public class GFG {


public static void main(String[] args)
{
Thread
.setDefaultUncaughtExceptionHandler(
new Random());

System.out.println(10 / 0);
}
}

Output:
Welcome to my World
Try

public class Main {


public static void main(String[ ] args) {
try {
int[] myNumbers = {1, 2, 3};
System.out.println(myNumbers[10]);
} catch (Exception e) {
System.out.println("Something went wrong."); }
}
}
Output
Something went wrong.

Multiple Catch Statements


class Main {
public static void main(String[] args) {
try {
int array[] = new int[10];
array[10] = 30 / 0;
} catch (ArithmeticException e) {
System.out.println(e.getMessage());
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println(e.getMessage());
}
}
}

Output:
/ by zero

Throw
class ThrowExcep
{
static void fun()
{
try
{
throw new NullPointerException("demo");
}
catch(NullPointerException e)
{
System.out.println("Caught inside fun().");
throw e; // rethrowing the exception
}
}
public static void main(String args[])
{
try
{
fun();
}
catch(NullPointerException e)
{
System.out.println("Caught in main.");
}
}
}

Output:

Caught inside fun().


Caught in main.
Program 5
Develop applications involving file handling: I/O streams,
File I/O.
I/O Streams
import java.io.*;
classDemo_print {
public static void main(String[] args)
{
System.out.print("GfG! ");
System.out.print("GfG! ");
System.out.print("GfG! ");
}
}

Output:
GfG! GfG! GfG!

File I/O
importjava.io.File;
public class ReadDir {
public static void main(String[] args) {
File file = null;
String[] paths;

try {
// create new file object
file = new File("/tmp");
// array of files and directory
paths = file.list();
// for each name in the path array
for(String path:paths) {
// prints filename and directory name
System.out.println(path);
}
} catch (Exception e) {
// if any error occurs
e.printStackTrace();
}
}
}

Output:
test1.txt
test2.txt
ReadDir.java
ReadDir.class

You might also like