SlideShare a Scribd company logo
1 of 12
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
Java Tutoring Exercises




                          Author: Uday Sharma |   11

More Related Content

What's hot

Advanced Java Practical File
Advanced Java Practical FileAdvanced Java Practical File
Advanced Java Practical File
Soumya Behera
 
Five class-based views everyone has written by now
Five class-based views everyone has written by nowFive class-based views everyone has written by now
Five class-based views everyone has written by now
James Aylett
 
Important java programs(collection+file)
Important java programs(collection+file)Important java programs(collection+file)
Important java programs(collection+file)
Alok Kumar
 
Dependency Inversion and Dependency Injection in PHP
Dependency Inversion and Dependency Injection in PHPDependency Inversion and Dependency Injection in PHP
Dependency Inversion and Dependency Injection in PHP
mtoppa
 
Professional JavaScript Development - Creating Reusable Code
Professional JavaScript Development -  Creating Reusable CodeProfessional JavaScript Development -  Creating Reusable Code
Professional JavaScript Development - Creating Reusable Code
Wildan Maulana
 

What's hot (20)

JavaScript Patterns
JavaScript PatternsJavaScript Patterns
JavaScript Patterns
 
Advanced Java Practical File
Advanced Java Practical FileAdvanced Java Practical File
Advanced Java Practical File
 
Python Programming Essentials - M20 - Classes and Objects
Python Programming Essentials - M20 - Classes and ObjectsPython Programming Essentials - M20 - Classes and Objects
Python Programming Essentials - M20 - Classes and Objects
 
Five class-based views everyone has written by now
Five class-based views everyone has written by nowFive class-based views everyone has written by now
Five class-based views everyone has written by now
 
Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
 
Chapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part IChapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part I
 
Javascript Design Patterns
Javascript Design PatternsJavascript Design Patterns
Javascript Design Patterns
 
Important java programs(collection+file)
Important java programs(collection+file)Important java programs(collection+file)
Important java programs(collection+file)
 
Sam wd programs
Sam wd programsSam wd programs
Sam wd programs
 
Java practical
Java practicalJava practical
Java practical
 
Java programs
Java programsJava programs
Java programs
 
Dependency Inversion and Dependency Injection in PHP
Dependency Inversion and Dependency Injection in PHPDependency Inversion and Dependency Injection in PHP
Dependency Inversion and Dependency Injection in PHP
 
Object Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsObject Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ Exams
 
Declarative Data Modeling in Python
Declarative Data Modeling in PythonDeclarative Data Modeling in Python
Declarative Data Modeling in Python
 
iOS Memory Management Basics
iOS Memory Management BasicsiOS Memory Management Basics
iOS Memory Management Basics
 
iOS 101 - Xcode, Objective-C, iOS APIs
iOS 101 - Xcode, Objective-C, iOS APIsiOS 101 - Xcode, Objective-C, iOS APIs
iOS 101 - Xcode, Objective-C, iOS APIs
 
java classes
java classesjava classes
java classes
 
Professional JavaScript Development - Creating Reusable Code
Professional JavaScript Development -  Creating Reusable CodeProfessional JavaScript Development -  Creating Reusable Code
Professional JavaScript Development - Creating Reusable Code
 
Constructors
ConstructorsConstructors
Constructors
 
Spsl v unit - final
Spsl v unit - finalSpsl v unit - final
Spsl v unit - final
 

Similar to Exercises of java tutoring -version1

@author public class Person{   String sname, .pdf
  @author   public class Person{   String sname, .pdf  @author   public class Person{   String sname, .pdf
@author public class Person{   String sname, .pdf
aplolomedicalstoremr
 
Can someone help me with this code When I run it, it stops after th.pdf
Can someone help me with this code When I run it, it stops after th.pdfCan someone help me with this code When I run it, it stops after th.pdf
Can someone help me with this code When I run it, it stops after th.pdf
Amansupan
 
Week 2 - Advanced C++list1.txt312220131197.docx
Week 2 - Advanced C++list1.txt312220131197.docxWeek 2 - Advanced C++list1.txt312220131197.docx
Week 2 - Advanced C++list1.txt312220131197.docx
melbruce90096
 
Create a Code that will add an Add, Edi, and Delete button to the GU.pdf
Create a Code that will add an Add, Edi, and Delete button to the GU.pdfCreate a Code that will add an Add, Edi, and Delete button to the GU.pdf
Create a Code that will add an Add, Edi, and Delete button to the GU.pdf
lakshmijewellery
 
Alcohol Awareness Special Lecture ReflectionAlcohol is among the.docx
Alcohol Awareness Special Lecture ReflectionAlcohol is among the.docxAlcohol Awareness Special Lecture ReflectionAlcohol is among the.docx
Alcohol Awareness Special Lecture ReflectionAlcohol is among the.docx
simonlbentley59018
 
Java căn bản - Chapter4
Java căn bản - Chapter4Java căn bản - Chapter4
Java căn bản - Chapter4
Vince Vo
 
Salesforce, APEX Concepts
Salesforce, APEX ConceptsSalesforce, APEX Concepts
Salesforce, APEX Concepts
Gaurish Goel
 

Similar to Exercises of java tutoring -version1 (20)

Keywords of java
Keywords of javaKeywords of java
Keywords of java
 
Class 6 2ciclo
Class 6 2cicloClass 6 2ciclo
Class 6 2ciclo
 
@author public class Person{   String sname, .pdf
  @author   public class Person{   String sname, .pdf  @author   public class Person{   String sname, .pdf
@author public class Person{   String sname, .pdf
 
2. Create a Java class called EmployeeMain within the same project Pr.docx
 2. Create a Java class called EmployeeMain within the same project Pr.docx 2. Create a Java class called EmployeeMain within the same project Pr.docx
2. Create a Java class called EmployeeMain within the same project Pr.docx
 
Unit-3 Practice Programs-5.docx
Unit-3 Practice Programs-5.docxUnit-3 Practice Programs-5.docx
Unit-3 Practice Programs-5.docx
 
Java2
Java2Java2
Java2
 
Test program
Test programTest program
Test program
 
Computer programming 2 -lesson 4
Computer programming 2  -lesson 4Computer programming 2  -lesson 4
Computer programming 2 -lesson 4
 
Write a class called Fraction with the two instance variables denomin.docx
 Write a class called Fraction with the two instance variables denomin.docx Write a class called Fraction with the two instance variables denomin.docx
Write a class called Fraction with the two instance variables denomin.docx
 
UNIT_-II_2021R.pptx
UNIT_-II_2021R.pptxUNIT_-II_2021R.pptx
UNIT_-II_2021R.pptx
 
classes & objects.ppt
classes & objects.pptclasses & objects.ppt
classes & objects.ppt
 
Can someone help me with this code When I run it, it stops after th.pdf
Can someone help me with this code When I run it, it stops after th.pdfCan someone help me with this code When I run it, it stops after th.pdf
Can someone help me with this code When I run it, it stops after th.pdf
 
Week 2 - Advanced C++list1.txt312220131197.docx
Week 2 - Advanced C++list1.txt312220131197.docxWeek 2 - Advanced C++list1.txt312220131197.docx
Week 2 - Advanced C++list1.txt312220131197.docx
 
Create a Code that will add an Add, Edi, and Delete button to the GU.pdf
Create a Code that will add an Add, Edi, and Delete button to the GU.pdfCreate a Code that will add an Add, Edi, and Delete button to the GU.pdf
Create a Code that will add an Add, Edi, and Delete button to the GU.pdf
 
Java doc Pr ITM2
Java doc Pr ITM2Java doc Pr ITM2
Java doc Pr ITM2
 
Week9 Intro to classes and objects in Java
Week9 Intro to classes and objects in JavaWeek9 Intro to classes and objects in Java
Week9 Intro to classes and objects in Java
 
Alcohol Awareness Special Lecture ReflectionAlcohol is among the.docx
Alcohol Awareness Special Lecture ReflectionAlcohol is among the.docxAlcohol Awareness Special Lecture ReflectionAlcohol is among the.docx
Alcohol Awareness Special Lecture ReflectionAlcohol is among the.docx
 
Java căn bản - Chapter4
Java căn bản - Chapter4Java căn bản - Chapter4
Java căn bản - Chapter4
 
Java Programs
Java ProgramsJava Programs
Java Programs
 
Salesforce, APEX Concepts
Salesforce, APEX ConceptsSalesforce, APEX Concepts
Salesforce, APEX Concepts
 

More from Uday Sharma (11)

Tftp client server communication
Tftp client server communicationTftp client server communication
Tftp client server communication
 
Wat question papers
Wat question papersWat question papers
Wat question papers
 
Fundamentals of java --- version 2
Fundamentals of java --- version 2Fundamentals of java --- version 2
Fundamentals of java --- version 2
 
Core java
Core javaCore java
Core java
 
Java tutor oo ps introduction-version 1
Java tutor  oo ps introduction-version 1Java tutor  oo ps introduction-version 1
Java tutor oo ps introduction-version 1
 
Logistics final prefinal
Logistics final prefinalLogistics final prefinal
Logistics final prefinal
 
Presentation1
Presentation1Presentation1
Presentation1
 
Parallel Programming
Parallel ProgrammingParallel Programming
Parallel Programming
 
Making Rules Project Management
Making Rules  Project ManagementMaking Rules  Project Management
Making Rules Project Management
 
Intelligent Weather Service
Intelligent Weather Service Intelligent Weather Service
Intelligent Weather Service
 
India presentation
India presentationIndia presentation
India presentation
 

Recently uploaded

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 

Recently uploaded (20)

FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 

Exercises of java tutoring -version1

  • 1. 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
  • 2. 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
  • 3. 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
  • 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 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
  • 5. 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
  • 6. 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
  • 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. 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
  • 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.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
  • 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.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
  • 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 : 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
  • 12. Java Tutoring Exercises Author: Uday Sharma | 11