Question

-------------------------------------------------------------------- 1. How does Java support the concept of encapsulation? -------------------------------------------------------------------- 2. Describe the difference between...

--------------------------------------------------------------------
1. How does Java support the concept of encapsulation?

--------------------------------------------------------------------
2. Describe the difference between an object and a class.

--------------------------------------------------------------------
3. What is the difference between the contents of a Java variable
of a primitive type and a Java variable of a class type?

--------------------------------------------------------------------
4. (a) How is a 'static' class method different from a regular
(non-static) class method?

(b) How is a 'static' variable in a class different from a
regular (instance) variable in a class?

--------------------------------------------------------------------
5. What does the Java keyword 'this' refer to?

--------------------------------------------------------------------
6. What does the Java 'new' operator do?

--------------------------------------------------------------------
7. When does a class require an 'equals' method to be written?

--------------------------------------------------------------------
8. What is method "overriding"? Why would you override a method?

--------------------------------------------------------------------
9. What advantages does inheritance provide in Java?

--------------------------------------------------------------------
10. What is dynamic binding? What advantage does it provide?

--------------------------------------------------------------------
11. How is it possible for two String objects with identical values
not to be equal when compared with the == operator?

--------------------------------------------------------------------
12. Why can a class variable of a base type be set to reference an
object of a derived type, eg:

Shape s = new Circle("Blue", 1.0);

--------------------------------------------------------------------
13. How can the methods of a derived class access private instance
variables of its base class?

--------------------------------------------------------------------
14. The following try/catch has code to explicitly handle any
IllegalArgumentException thrown. Show the modified code that
will also catch any other type of exception that could be thrown
by code called within the try block.

try
{
... methods called here ...
...
}
catch (IllegalArgumentException e)
{
... code to handle IllegalArgumentException ...
}

--------------------------------------------------------------------
15. How is the caller's flow of control (sequence of statement
execution) different when an exception is thrown from a method
that it calls, vs. when the method returns normally?

-------------------------------------------------------------------


The following questions are 20 points each. Paste your source code
below each question, and example input and output. Do not include
the code required to test the methods, etc.

--------------------------------------------------------------------
P1. Write a static method that checks if a String represents a valid
integer value and returns true if valid, or false if not. For
example, "123", "-45", and "0" are valid integers, but "x7" and
"3.14" are not. Use java.lang.Integer.parseInt() to perform the
actual validity checking inside your method. Your method should
also return false if the parameter String is null or empty.
It should NOT throw any exceptions back to the caller (but may
use exceptions internally).

Use the following prototype for your method:

public static boolean isValidInt(String testIntval)

Show example output for the following values entered:
"42"
"-123"
"12-34"
"10.5"
"x7"
"" (length 0 string)
null (pass a Java null reference in a hardcoded call to isValidInt)

--------------------------------------------------------------------
P2. Write a Java method that returns the largest value passed to it.
Use the following prototype:

public static int max(int val1, int val2, int val3) {
// return the largest of the three values...
}

For example, the following call of your method sets 'n' to 30:

int n = max(20, 30, 10);

Note that any three values can be passed to this method.
The following would also set 'n' to 30:

int n = max(30, 30, 10);

Write a main() method to test your max method. It should input
three values, call your max method, and then print the three
values and returned max value as output, eg:

Input values: 30 20 10 - max value is 30
  
The max() method cannot read any input values, nor print any
output values. All input and printing is done by the main()
method that calls max().

Paste in both your source code for your max() method, and
the runtime output showing the parameter values and returned
maximum value for the following sample input values:

a) 30, 30, 30
b) 10, -10, 20
c) 40, 60, 50
d) 90, 70, 80
e) 50, 50, 25
f) 50, 50, 75
g) 200, 100, 200
h) 200, 400, 200
i) 300, 500, 500
j) 700, 500, 500

--------------------------------------------------------------------

0 0
Add a comment Improve this question Transcribed image text
Answer #1

You have posted too many questions I will try to answer as much as i can as per the time provided to me

1)

Encapsulation means that all the properties and functionalities related to some object are bundled together and one should not be able to access it from any other part of the code Java allows this by the use of class

2)

Object represents a real world entity Memory is allocated on creating an object

Class is a blueprint of an object which defines the properties and methods that can be used to access those properties

5)

this keywoord is an instance member variable which is inbuilt inside every function It can be used to point to the calling object

6)

new operator assigns memory fron the free area  

9)

By using inheritance, one need not define same of the methods and properties as already defined in super class

p2)

Below is the JAVA code I hope that i have provided sufficient comments for your better understanding Note that I have done proper indentation but this code is automatically left alligned on this interface

import java.util.Scanner;
public class Main
{
public static int max(int val1, int val2, int val3)
{
int result = val1; //initialize with 1st value
if(result<val2) //compare with 2nd value
result=val2; //update if required
if(result<val3) //compare with 3rd value
result=val3; //update if required
return result;
}
public static void main(String []args)
{
Scanner scan = new Scanner(System.in);
System.out.print("Input values:");
//Take user input
int val1 = scan.nextInt();
int val2 = scan.nextInt();
int val3 = scan.nextInt();
System.out.println("max value is "+max(val1, val2, val3)); //call the function
}
}

I have tried to explain it in very simple language and I hope that i have answered your question satisfactorily.Leave doubts in comment section if any

Add a comment
Know the answer?
Add Answer to:
-------------------------------------------------------------------- 1. How does Java support the concept of encapsulation? -------------------------------------------------------------------- 2. Describe the difference between...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA PROG HW Problem 1 1. In the src −→ edu.neiu.p2 directory, create a package named...

    JAVA PROG HW Problem 1 1. In the src −→ edu.neiu.p2 directory, create a package named problem1. 2. Create a Java class named StringParser with the following: ApublicstaticmethodnamedfindIntegerthattakesaStringandtwocharvariables as parameters (in that order) and does not return anything. The method should find and print the integer value that is located in between the two characters. You can assume that the second char parameter will always follow the firstchar parameter. However, you cannot assume that the parameters will be different from...

  • Problem 2 1. In the src → edu.neiu.p2 directory, create a package named problem1. 2. Create...

    Problem 2 1. In the src → edu.neiu.p2 directory, create a package named problem1. 2. Create a Java class named Complement with the following: • A public static method named onesComplement that takes a String as a parameter and returns a String that is the 1’s complement of the parameter. • If the String is not a valid binary number (i.e. only has 0s and 1s), you should throw an IllegalArgumentException with the message "Not a valid binary number". •...

  • Java Question, I need a program that asks a user to input a string && then...

    Java Question, I need a program that asks a user to input a string && then asks the user to type in an index value(integer). You will use the charAt( ) method from the string class to find and output the character referenced by that index. Allow the user to repeat these actions by placing this in a loop until the user gives you an empty string. Now realize that If we call the charAt method with a bad value...

  • in java, finish the method 17: Chapter 8: Handling a number as different types (Method Overloading)...

    in java, finish the method 17: Chapter 8: Handling a number as different types (Method Overloading) J o pulli UCILISIS lule zyBooks catalog Help/FAQ 31.47 Chapter 8: Handling a number as different types (Method Overloading) Write an overloaded method called divide_by_two() that can handle a number between 1-5 as an int, double, or String. • The method should be able to handle the String version with upper or lower case letters in any location in the String. ACTIVITY 31.47.1: Chapter...

  • need java code for this question Question 2 (15 marks) (a) Does the following class successfully...

    need java code for this question Question 2 (15 marks) (a) Does the following class successfully compile? Explain your answer. public class MyClass public static void main(String arge) if(Integer.parseInt(args[0]) < 0) throw new RuntimeException(); C { 1 } If the class does compile, describe what will happen when we run it with command: java MyClass -10 (5 marks) (b) Write a complete definition of the method with the heading given below: public static double calculate insurance Premium double carValue, int...

  • I need to create a code for this prompt: In this project we will build a...

    I need to create a code for this prompt: In this project we will build a generic UserInput class for getting keyboard input from the user. Implementation: The class UserInput is a 'Methods only' class, and all the methods should be declared static. Look at the TestScanner.java program at the bottom of this page that inputs a string, int and double. It shows you how to use Scanner class to get input from the keyboard. Write FOUR simple methods, one...

  • IT Java code In Lab 8, we are going to re-write Lab 3 and add code...

    IT Java code In Lab 8, we are going to re-write Lab 3 and add code to validate user input. The Body Mass Index (BMI) is a calculation used to categorize whether a person’s weight is at a healthy level for a given height. The formula is as follows:                 bmi = kilograms / (meters2)                 where kilograms = person’s weight in kilograms, meters = person’s height in meters BMI is then categorized as follows: Classification BMI Range Underweight Less...

  • pls help java ASAP!!!!!!! Topic String Tokenizer Static Methods Static Variables Primitive Arrays Description Enhance the...

    pls help java ASAP!!!!!!! Topic String Tokenizer Static Methods Static Variables Primitive Arrays Description Enhance the last assignment by providing the following additional features: (The additional features are listed in bold below) Class Statistics In the class Statistics, create the following static methods (in addition to the instance methods already provided). • A public static method for computing sorted data. • A public static method for computing min value. • A public static method for computing max value. • A...

  • How would I code this method in Java using a scanner? Begin by asking how many...

    How would I code this method in Java using a scanner? Begin by asking how many spaces the row should be with a prompt using System.out.print like the following, with 40 as the maximally allowed number of spaces: (user input shown bold) Please enter the number of spaces for the game (1-40): eight To do this, use the promptNumberReadLine method that you will write, described on the back page. If the user does not type a number in the correct...

  • 1) Consider the following Java program: 1 public class HelloWorld { 2     // My first program!...

    1) Consider the following Java program: 1 public class HelloWorld { 2     // My first program! 3     public static void main(String[] args) { 4         System.out.println("Hello, World!"); 5     } 6 } What is on line 1? a. a variable declaration b. a statement c. a method (subroutine) definition d. a comment e. a class definition 2) Which one of the following does NOT describe an array? a. It can be used in a for-each loop. b. It has a numbered sequence...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT