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

JAVA Homework help thank you! Question 1: Methods and Variables Answer at least

ID: 3679567 • Letter: J

Question

JAVA Homework help thank you!

Question 1: Methods and Variables

Answer at least 3 of the following:

1.What is specified in the method header?

2.What is a formal parameter, actual parameter, local variable, and instance variable?

3.What is method overloading?

4.What is encapsulation? How do you support encapsulation?

5.What is a static variable and how is it different from an instance variable?

6.What is a static method and how is it different from a non-static (instance) method?

7.How do you invoke a static method?

Question 2: Writing Methods

1.Write a complete method that takes in two numbers. The method returns true if the second number is a multiple of the first. For example, calling the method will 3, 6 or with 2, 8 will return true. Calling with 3, 10 will return false.

2.Overload this method and write a method that takes in five numbers. The method returns true if the second, third, fourth, and fifth number are all multiples of the first. For example, calling the method with 3, 6, 12, 9, 15 will return true. Calling with 3, 6, 10, 9, 15 will return false.

Question 3: Coding Random

1.Write code to generate a random number between 0 and 100 inclusive.

2.Write code to generate a random number between -10 and 10 exclusive.

3.What is the range of numbers generated by:

Random generator = new Random();

generator.nextInt(13) - 6;

4.What is the range of numbers generated by:

Random generator = new Random();

generator.nextInt(9) + 3;

Question4: Strings

1.What is stored in aString and bString after each of the following?

1.String aString = "Studying is lots of fun."; String bString = aString.substring(4, 14);

2.String aString = "Java is great."; String bString = aString.toUpperCase();

3.String aString = "I love Java"; String bString = aString.substring(2).toLowerCase();

4.String aString = "Midterms stress me out"; String bString = aString.substring( (aString.length()/2)-2, 15);

2.What is the output of the following?

1.System.out.println(3 + 4 + 5);

2.System.out.println("3" + 4 + 5);

3.System.out.println(3 + 4 + "5");

4.System.out.println(3 + 4 + "5" + "6");

Question 5: Arrays

1.Are arrays primitives or objects? How does Java store arrays in memory?

2.Write a method that takes in two parameters: an integer array and a number. Increment each element in the array by the specified number.

Explanation / Answer

Question1

2...

formal parameter
   the identifier used in a method to stand for the value that is passed into the method by a caller.
For example, amount is a formal parameter of processDeposit

actual parameter
   the actual value that is passed into the method by a caller.
For example, the 200 used when processDeposit is called is an actual parameter.
actual parameters are often called arguments.


Local variables:
   Local variables are declared in methods, constructors, or blocks.

   Local variables are created when the method, constructor or block is entered and    the variable will be destroyed once it exits the method, constructor or block.

   Access modifiers cannot be used for local variables.

   Local variables are visible only within the declared method, constructor or block.
Instance variables:
   Instance variables are declared in a class, but outside a method, constructor or    any block.

   When a space is allocated for an object in the heap, a slot for each instance    variable value is created.

   Instance variables are created when an object is created with the use of the    keyword 'new' and destroyed when the object is destroyed.

3....

Overloading:

If a class have multiple methods by same name but different parameters, it is known as Method Overloading.

If we have to perform only one operation, having same name of the methods increases the readability of the program.

Suppose you have to perform addition of the given numbers but there can be any number of arguments, if you write the method such as a(int,int) for two parameters, and b(int,int,int) for three parameters then it may be difficult for you as well as other programmers to understand the behaviour of the method because its name differs. So, we perform method overloading to figure out the program quickly

4.....

Encapsulation in Java

Encapsulation in java is a process of wrapping code and data together into a single unit, for example capsule i.e. mixed of several medicines.

We can create a fully encapsulated class in java by making all the data members of the class private. Now we can use setter and getter methods to set and get the data in it.

The Java Bean class is the example of fully encapsulated class.