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

How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
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
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 

Recently uploaded (20)

How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
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 ...
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 

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