Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Question 1: Create a java program that will take an input from the keyboard to a

ID: 3861371 • Letter: Q

Question

Question 1:

Create a java program that will take an input from the keyboard to add, subtract, multiply and divide two numbers of type double

Question 2:

Create a program that will call a dumb method, a clever method and a smart method.

Explain the differences between, dumb method, clever method and a smart method.

Question 3:

Create a program to demonstrate the differences between different types of access modifiers in java (public, protected, default, private)

Explain the visibility of each modifier.

Question 4:

Define the following terms:

Class

Object

Method

Local variable

Instance variable

Variable scope

Primitive datatype

User-defined datatypes

Pass by value vs. pass by reference

Syntax error

Logic error

Lastly, why java is popular?

Thank you.

Explanation / Answer

Q1.Create a java program that will take an input from the keyboard to add, subtract, multiply and divide two numbers of type double.

import java.util.Scanner;

public class Calculate

{

   public static void main(String[] args)

    {

        double m, n, opt, add, sub, mul;

        double div;

        Scanner s = new Scanner(System.in);

        System.out.print("Enter first double number:");

        m = s.nextInt();

        System.out.print("Enter second double number:");

        n = s.nextInt();

        while(true)

        {

            System.out.println("Enter a for addition");

            System.out.println("Enter b for subtraction");

            System.out.println("Enter c for multiplication");

            System.out.println("Enter d for division");

            System.out.println("Enter e to Exit");

            opt = s.nextInt();

            switch(opt)

            {

                case a:

                add = m + n;

                System.out.println("Result:"+add);

                break;

                case b:

                sub = m - n;

                System.out.println("Result:"+sub);

                break;

                case c:

                mul = m * n;

                System.out.println("Result:"+mul);

                break;

                case d:

                div = (double)m / n;

                System.out.println("Result:"+div);

                break;   

                case e:

                System.exit(0);

            }

        }

       }

    }

Q3.Create a program to demonstrate the differences between different types of access modifiers in java (public, protected, default, private)

Explain the visibility of each modifier.

Access Specifier/Modifier in any language defines the boundary and scope for accessing the method, variable and class etc.

Java has defined four types of access specifiers. These are :

1.    Public

2.    Private

3.    Protected

4.    Default

If you do not define any access specifier/modifier than java will take “default” access specifier as the default for variables/methods/class.

SYNTAX For declaring a class with access specifier :

<access-specifier><class-keyword><class-name>

For e.g.

public class Demo

public access specifier :

If you declare any method/function as the public access specifier than that variable/method can be used anywhere.

public Access Specifier Example :

package abc;   

public class AccessDemo

{

  

      public void test()

      {

            System.out.println("Example of public access specifier");

      }

}

In any other package if we want to access the class "AccessDemo" then we only need to import the abc package as shown in the following code :

package xyz;

import abc.AccessDemo;

public class AccessExample

{

  

      public static void main(String[] args)

      {

           AccessDemo ad = new AccessDemo();

           ad.test();

      }

}

private access specifier :

If you declare any method/variable as private than it will only accessed in the same class which declare that method/variable as private. The private members cannot access in the outside world. If any class declared as private than that class will not be inherited. :

private Access Specifier Example :

class AccessDemo  

{

       private int x = 56;

  

      public void showDemo()  

      {

           System.out.println("The Variable value is " + x);

      }

   

      private void testDemo()  

      {

           System.out.println("It cannot be accessed in another class");

      }

}

   

  

public class AccessExample  

{

   

      public static void main(String[] args)  

      {

           AccessDemo ad = new AccessDemo();

           ad.testDemo(); // Private method cannot be used

           ad.x = 5; // Private variable cannot be used

   

           ad.showDemo(); // run properly

      }

}

  

default access specifier :

If you define any method/variable as default or not give any access specifier than the method or variable will be accessed in only that package in which they are defined.

They cannot be accessed outside the package.

default Access Specifier Example :

In one package we define the variable default as below code :

    package abc;

  

class AccessDemo  

{

      default int a = 4;

}

Now in any other package if we want to change the value of a, it is not possible because the other package has not access permission for "a" variable.

package xyz;

import abc.AccessDemo;

  

  

class AccessExample  

{

      public static void main(String[] args)

      {

           AccessDemo ad = new AccessDemo();

           ad.a = 67; //It is not possible.

      }

}

protected access specifier :

It has same properties as that of private access specifier but it can access the methods/variables in the child class of parent class in which they are declared as protected.

When we want to use the private members of parent class in the child class then we declare those variables as protected.

protected Access Specifier Example :

class AccessDemo  

{

      protected int x = 34;

   

       public void showDemo()  

      {

            System.out.println("The variable value is " + x);

       }

}

   

class ChildAccess extends AccessDemo

{

       // child class which inherits

       // the properties of AccessDemo class

}

   

public class AccessExample  

{

   

       public static void main(String[] args)  

      {

            ChildAccess ca = new ChildAccess();

   

            ca.showDemo(); // run properly

            ca.x = 45; // run properly

}  

}

Q4. Define the following terms:

Class

A class is nothing but a blueprint or a template for creating different objects which defines its properties and behaviors.

Object

Java class objects exhibit the properties and behaviors defined by its class. A class can contain fields and methods to describe the behavior of an object.

Method

A method is a set of code which is referred to by name and can be called (invoked) at any point in a program simply by utilizing the method's name. Think of a method as a subprogram that acts on data and often returns a value. Each method has its own name.

Local variable

A local variable in Java is a variable that's declared within the body of a method. Then you can use the variable only within that method. Other methods in the class aren't even aware that the variable exists.

Instance variable

Instance variables are used by Objects to store their states. Variables which are defined without the STATIC keyword and are Outside any method declaration are Object specific and are known as instance variables. They are called so because their values are instance specific and are not shared among instances.

Variable scope

Scope and Lifetime of Variables. The scope of a variable defines the section of the code in which the variable is visible. As a general rule, variables that are defined within a block are not accessible outside that block. The lifetime of a variable refers to how long the variable exists before it is destroyed.

Primitive datatype

Primitive types are the most basic data types available within the Java language. There are 8: boolean , byte , char , short , int , long , float and double . These types serve as the building blocks of data manipulation in Java. Such types serve only one purpose — containing pure, simple values of a kind.

User-defined datatypes

Primitive data types are the general and fundamental data types that we have in Java and those are byte, short, int, long, float, double, char, boolean . Derived data types are those that are made by using any other data type for example, arrays. User defined data types are those that user / programmer himself defines.

Pass by value vs. pass by reference

Passing by reference means the called functions' parameter will be the same as the callers' passed argument (not the value, but the identity - the variable itself). Pass by value means the called functions' parameter will be a copy of the callers' passed argument.

Passing by reference means the called functions' parameter will be the same as the callers' passed argument (not the value, but the identity - the variable itself). Pass by value means the called functions' parameter will be a copy of the callers' passed argument.

Java does manipulate objects by reference, and all object variables are references. However, Java doesn't pass method arguments by reference; it passes them by value.

Syntax error

In computer science, a syntax error is an error in the syntax of a sequence of characters or tokens that is intended to be written in a particular programming language. For compiled languages, syntax errors are detected at compile-time. A program will not compile until all syntax errors are corrected.

Logic error

In computer programming, a logic error is a bug in a program that causes it to operate incorrectly, but not to terminate abnormally (or crash). A logic error produces unintended or undesired output or other behavior, although it may not immediately be recognized as suc

Lastly, why java is popular?

Java applets feature in millions of websites, almost all computer systems globally feature JRE and many applications initially developed using other languages often incorporate Java-encoded applications at a later date due to enhanced security/stability requirements. The list of applications in which Java codes feature is almost endless, even Google’s Android OS originally featured a Java kernel, which led to a copyright infringement lawsuit between Google and Oracle. Discussing the advantages and limitations of the Java language could shed some more light on the reasons for its continued popularity:

Advantages of Java
The primary advantage of Java application development is that it is free and its syntax bears resemblance to various C-based programming languages, making it easier for developers to understand and implement. The language also features an extensive standard class library, most of which is quite well written. The Java developer kit includes tools for automatic memory management, while simultaneously providing a sturdy platform for behavioral transference from one address space to another. The language also provides superior performance as compared to many available languages, along with better portability and a simplified syntax as compared to the C++ language. Other advantages of java software development include the non-committee driven language design, presence of the No Explicit Pointers tool and the availability of Explicit Interfaces to help the developer write error-free coding. Java application development also facilitates the availability of comprehensive documentation as well as numerous freely available sources for third-party libraries and codes. Developers also have a wide range of Java IDE choices, while remaining unaffected by the problem of a Fragile Binary Interface. Considering these benefits, it is no surprise that Java assumed supreme popularity especially during the initial years following its introduction.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote