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 FileSoumya Behera
 
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 ObjectsP3 InfoTech Solutions Pvt. Ltd.
 
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 nowJames Aylett
 
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 IEduardo Bergavera
 
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 PHPmtoppa
 
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++ ExamsMuhammadTalha436
 
Declarative Data Modeling in Python
Declarative Data Modeling in PythonDeclarative Data Modeling in Python
Declarative Data Modeling in PythonJoshua Forman
 
iOS Memory Management Basics
iOS Memory Management BasicsiOS Memory Management Basics
iOS Memory Management BasicsBilue
 
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 APIsSubhransu Behera
 
Professional JavaScript Development - Creating Reusable Code
Professional JavaScript Development -  Creating Reusable CodeProfessional JavaScript Development -  Creating Reusable Code
Professional JavaScript Development - Creating Reusable CodeWildan 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

Keywords of java
Keywords of javaKeywords of java
Keywords of javaJani Harsh
 
@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, .pdfaplolomedicalstoremr
 
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.docxajoy21
 
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.docxajoy21
 
UNIT_-II_2021R.pptx
UNIT_-II_2021R.pptxUNIT_-II_2021R.pptx
UNIT_-II_2021R.pptxRDeepa9
 
classes & objects.ppt
classes & objects.pptclasses & objects.ppt
classes & objects.pptBArulmozhi
 
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.pdfAmansupan
 
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.docxmelbruce90096
 
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.pdflakshmijewellery
 
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 Javakjkleindorfer
 
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.docxsimonlbentley59018
 
Java căn bản - Chapter4
Java căn bản - Chapter4Java căn bản - Chapter4
Java căn bản - Chapter4Vince Vo
 
Java Programs
Java ProgramsJava Programs
Java Programsvvpadhu
 
Salesforce, APEX Concepts
Salesforce, APEX ConceptsSalesforce, APEX Concepts
Salesforce, APEX ConceptsGaurish 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

Tftp client server communication
Tftp client server communicationTftp client server communication
Tftp client server communicationUday Sharma
 
Wat question papers
Wat question papersWat question papers
Wat question papersUday Sharma
 
Fundamentals of java --- version 2
Fundamentals of java --- version 2Fundamentals of java --- version 2
Fundamentals of java --- version 2Uday Sharma
 
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 1Uday Sharma
 
Logistics final prefinal
Logistics final prefinalLogistics final prefinal
Logistics final prefinalUday Sharma
 
Parallel Programming
Parallel ProgrammingParallel Programming
Parallel ProgrammingUday Sharma
 
Making Rules Project Management
Making Rules  Project ManagementMaking Rules  Project Management
Making Rules Project ManagementUday Sharma
 
Intelligent Weather Service
Intelligent Weather Service Intelligent Weather Service
Intelligent Weather Service Uday Sharma
 
India presentation
India presentationIndia presentation
India presentationUday Sharma
 

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

Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 
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...Poonam Aher Patil
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
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 17Celine George
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfChris Hunter
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIShubhangi Sonawane
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 

Recently uploaded (20)

Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
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...
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
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
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 

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