Java TutoringExercisesJava tutoring programs and outputsThis document covers basic Java examples which shows how to build ...
Java Tutoring ExercisesExercise 1:Basic Calculator/** * * @author Udaysharma * This is simple basic java program which per...
Java Tutoring Exercises}}OutputAddition is C=20Subtraction is C=0Multiplication is C=100Division is C=1Exercise 2: Basic C...
Java Tutoring Exercises              * a,b : Is a parameter passing to the Addition class method    Addition->add(int a,in...
Java Tutoring ExercisesSubtraction class : Subtraction.java/** * * @author Udaysharma * Class Subtraction which perform su...
Java Tutoring ExercisesExercise 3: Basic Calculator usingInheritanceBase Class : Calculator.java/** * * @author Udaysharma...
Java Tutoring Exercises      }}Sub class : Addition.java/** * * @author Udaysharma * Class Addition is sub class of Calcul...
Java Tutoring Exercises      int c;      c=this.a-this.b;      System.out.println("Subtraction is C="+c);}}Output:Addition...
Java Tutoring Exercises              *              */             addition.add();             System.out.println("/******...
Java Tutoring Exercises          */      public void add() {             // Store result in c             int c = 0;      ...
Java Tutoring Exercises      BufferedReader reader = new BufferedReader(new      InputStreamReader(System.in));      /**  ...
Java Tutoring Exercises                          Author: Uday Sharma |   11
Upcoming SlideShare
Loading in...5
×

Exercises of java tutoring -version1

869

Published on

Published in: Education
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
869
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Exercises of java tutoring -version1

  1. 1. Java TutoringExercisesJava tutoring programs and outputsThis document covers basic Java examples which shows how to build program using Pass ByValue and inheritance and how to read input from the keyboard. Author : Uday Sharma
  2. 2. Java Tutoring ExercisesExercise 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 variablestatic 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
  3. 3. Java Tutoring Exercises}}OutputAddition is C=20Subtraction is C=0Multiplication is C=100Division is C=1Exercise 2: Basic Calculator using Pass ByValueMain 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
  4. 4. 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 methodSubtraction->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
  5. 5. Java Tutoring ExercisesSubtraction 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);}}OutputAddition is C=20Subtraction is C=0 Author: Uday Sharma | 4
  6. 6. Java Tutoring ExercisesExercise 3: Basic Calculator usingInheritanceBase 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
  7. 7. 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
  8. 8. Java Tutoring Exercises int c; c=this.a-this.b; System.out.println("Subtraction is C="+c);}}Output:Addition is C=20Subtraction is C=0Exercise 4: Reading input from thekeyboardBase 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
  9. 9. 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.javaimport 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
  10. 10. 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.javaimport 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
  11. 11. 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 : 10Enter Value for b : 10Addition is C=20/*************************************//**************Subtraction***********************/Enter Value for a : 10Enter Value for b : 10Subtraction is C=0/*************************************/ Author: Uday Sharma | 10
  12. 12. Java Tutoring Exercises Author: Uday Sharma | 11

×