Java Tutoring
Exercises
Java tutoring programs and outputs
This document covers basic Java examples which shows how to build program using Pass By
Value and inheritance and how to read input from the keyboard.
Author : Uday Sharma
Java Tutoring Exercises
Exercise 1:Basic Calculator
/**
*
* @author Udaysharma
* This is simple basic java program which perform Addition, Substraction,
Multiplication and Division.
*/
public class BasicCalculator {
//Global static variable
//Static type variable : Are globally public variable
static int a =10;
static int b= 10;
/*
* Java Main method syntax
* Public : is a keyword which suggest main method is accessible by publicly
* Static : In java code static member executable first.
* Void : There is no return value.
* main : is a method which is execute beginning of the code.
* String : Data type
* args[] : which allows you to access Environment variable.
*/
public static void main(String args[])
{
//To store result.
int c;
/*********************Addition*******************/
c=a+b;
//To print result in console
System.out.println("Addition is C="+c);
/************************************************/
/*********************Subtraction*******************/
c=a-b;
//To print result in console
System.out.println("Subtraction is C="+c);
/***************************************************/
/*********************Multiplication*******************/
c=a*b;
//To print result in console
System.out.println("Multiplication is C="+c);
/******************************************************/
/*********************Division*******************/
c=a/b;
//To print result in console
System.out.println("Division is C="+c);
/******************************************************/
Author: Uday Sharma | 1
Java Tutoring Exercises
}
}
Output
Addition is C=20
Subtraction is C=0
Multiplication is C=100
Division is C=1
Exercise 2: Basic Calculator using Pass By
Value
Main class : Calculator.java
/**
*
* @author Udaysharma
* Basic calculator using Pass by Value
*/
public class Calculator {
//Global static variable
//Static type variable : Are globally public variable
static int a =10;
static int b= 10;
public static void main(String args[])
{
/**************Addition***********************/
/*
* Creating object for the Class addition
* Addition : Name of the class
* addition : Object Name
* new Addition : Default constructor of class Addition
*/
Addition addition = new Addition();
/*
* addition : Object Name
* add : is a Method of Class Addition
Author: Uday Sharma | 2
Java Tutoring Exercises
* a,b : Is a parameter passing to the Addition class method Addition-
>add(int a,int b)
*/
addition.add(a, b);
/********************************************/
/**************Subtraction***********************/
/*
* Creating object for the Class addition
* Subtraction : Name of the class
* subtraction : Object Name
* new Subtraction : Default constructor of class subtraction
*/
Subtraction subtraction = new Subtraction();
/*
* subtraction : Object Name
* sub : is a Method of Class Subtraction
* a,b : Is a parameter passing to the Subtraction class method
Subtraction->sub(int a,int b)
*/
subtraction.sub(a, b);
/********************************************/
}
}
Addition Class : Addition.java
/**
*
* @author Udaysharma
* Class Addition which perform addition of given two number
*/
public class Addition {
/*
* Method add accept parameter value a and b and
* then perform addition of two number
*/
public void add(int a, int b)
{
//Store result in c
int c;
c=a+b;
System.out.println("Addition is C="+c);
}
}
Author: Uday Sharma | 3
Java Tutoring Exercises
Subtraction class : Subtraction.java
/**
*
* @author Udaysharma
* Class Subtraction which perform subtraction of given two number
*/
public class Subtraction {
/*
* Method add accept parameter value a and b and
* then perform subtraction of two number
*/
public void sub(int a, int b)
{
//Store result in c
int c;
c=a-b;
System.out.println("Subtraction is C="+c);
}
}
Output
Addition is C=20
Subtraction is C=0
Author: Uday Sharma | 4
Java Tutoring Exercises
Exercise 3: Basic Calculator using
Inheritance
Base Class : Calculator.java
/**
*
* @author Udaysharma
* Basic calculator using Pass by Value
*/
public class Calculator {
//Protected type variable : Are accessible by the subclass of Calculator
protected int a =10;
protected int b= 10;
public static void main(String args[])
{
/**************Addition***********************/
/*
* Creating object for the Class addition
* Addition : Name of the class
* addition : Object Name
* new Addition : Default constructor of class Addition
*/
Addition addition = new Addition();
/*
* addition : Object Name
* add : is a Method of Class Addition
*
*/
addition.add();
/********************************************/
/**************Subtraction***********************/
/*
* Creating object for the Class addition
* Subtraction : Name of the class
* subtraction : Object Name
* new Subtraction : Default constructor of class subtraction
*/
Subtraction subtraction = new Subtraction();
/*
* subtraction : Object Name
* sub : is a Method of Class Subtraction
*
*/
subtraction.sub();
/********************************************/
Author: Uday Sharma | 5
Java Tutoring Exercises
}
}
Sub class : Addition.java
/**
*
* @author Udaysharma
* Class Addition is sub class of Calculator
* which perform addition of given two number
*/
public class Addition extends Calculator {
/*
* Method add accept parameter value a and b and
* then perform addition of two number
*/
public void add()
{
//Store result in c
int c;
c=this.a+this.b;
System.out.println("Addition is C="+c);
}
}
Sub class : Subtraction.java
/**
*
* @author Udaysharma
* Class Subtraction is a sub class of Calculator
* which perform subtraction of given two number
*/
public class Subtraction extends Calculator {
/*
* Method add accept parameter value a and b and
* then perform subtraction of two number
*/
public void sub()
{
//Store result in c
Author: Uday Sharma | 6
Java Tutoring Exercises
int c;
c=this.a-this.b;
System.out.println("Subtraction is C="+c);
}
}
Output:
Addition is C=20
Subtraction is C=0
Exercise 4: Reading input from the
keyboard
Base Class : Calculator.java
/**
*
* @author Udaysharma
* Basic calculator using Inheritance
*/
public class Calculator {
//Protected type variable : Are accessible by only the subclass of Calculator
protected int a =10;
protected int b= 10;
public static void main(String args[])
{
/**************Addition***********************/
/*
* Creating object for the Class addition
* Addition : Name of the class
* addition : Object Name
* new Addition : Default constructor of class Addition
*/
System.out.println("/**************Addition***********************/");
Addition addition = new Addition();
/*
* addition : Object Name
* add : is a Method of Class Addition
Author: Uday Sharma | 7
Java Tutoring Exercises
*
*/
addition.add();
System.out.println("/*************************************/");
/********************************************/
/**************Subtraction***********************/
/*
* Creating object for the Class addition
* Subtraction : Name of the class
* subtraction : Object Name
* new Subtraction : Default constructor of class subtraction
*/
System.out.println("/**************Subtraction***********************/");
Subtraction subtraction = new Subtraction();
/*
* subtraction : Object Name
* sub : is a Method of Class Subtraction
*
*/
subtraction.sub();
System.out.println("/*************************************/");
/********************************************/
}
}
Sub class : Addition.java
import java.io.BufferedReader;
import java.io.InputStreamReader;
/**
*
* @author Udaysharma Class Addition is sub class of Calculator which perform
* addition of given two number
*/
public class Addition extends Calculator {
/**
* BufferedReader: Read typed value from the Buffer InputStreamReader: Read
* typed key and store it into the buffer. System.in : Console input.
*/
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
/**
*
* Method add which perform addition of two number
Author: Uday Sharma | 8
Java Tutoring Exercises
*/
public void add() {
// Store result in c
int c = 0;
/*
* Try -catch block used for exception handling.
* (Ex. our program understand Digit but if you will give
* input as a Character than its generate error).
*/
try {
System.out.print("Enter Value for a : ");
//Read input from console and convert it into the Integer
this.a = Integer.parseInt(reader.readLine());
System.out.print("Enter Value for b : ");
//Read input from console and convert it into the Integer
this.b = Integer.parseInt(reader.readLine());
c = this.a + this.b;
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Addition is C=" + c);
}
}
Sub class : Subtraction.java
import java.io.BufferedReader;
import java.io.InputStreamReader;
/**
*
* @author Udaysharma Class Addition is sub class of Calculator which perform
* addition of given two number
*/
public class Addition extends Calculator {
/**
* BufferedReader: Read typed value from the Buffer InputStreamReader:
Read
* typed key and store it into the buffer. System.in : Console input.
*/
Author: Uday Sharma | 9
Java Tutoring Exercises
BufferedReader reader = new BufferedReader(new
InputStreamReader(System.in));
/**
*
* Method add which perform addition of two number
*/
public void add() {
// Store result in c
int c = 0;
/*
* Try -catch block used for exception handling.
* (Ex. our program understand Digit but if you will give
* input as a Character than its generate error).
*/
try {
System.out.print("Enter Value for a : ");
//Read input from console and convert it into the Integer
this.a = Integer.parseInt(reader.readLine());
System.out.print("Enter Value for b : ");
//Read input from console and convert it into the Integer
this.b = Integer.parseInt(reader.readLine());
c = this.a + this.b;
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Addition is C=" + c);
}
}
Output
/**************Addition***********************/
Enter Value for a : 10
Enter Value for b : 10
Addition is C=20
/*************************************/
/**************Subtraction***********************/
Enter Value for a : 10
Enter Value for b : 10
Subtraction is C=0
/*************************************/
Author: Uday Sharma | 10