Questions

1. What is the output of the following code?

int prod;
for (int x=2; x<=5; x++) {
for (int y=3; y<=6; y++) {
prod = x * y;
System.out.println(prod);
}
}

2. Given the following array:

int[ ] grades = { 90, 80, 70, 60, 65, 80, 50, 85, 95};
  • How many elements are there in the array?
  • Implement (code) the following method that will return the lowest value in the array.
  • Implement (code) the following method that will return the number of elements in the array that are less than the value passed in through the parameter list.

3. Given the following class:

class Quadrilateral {
public double calcPerimeter() {
//calculate the perimeter of the Quadrilateral (shape with four sides)
return 0.0;
}
}
  • Create a Square class that will inherit from the Quadrilateral class. Over ride the calcPerimeter() method and implement (code) the calcPerimeter () for the Square class.
  • Create a Rectangle class that will inherit from the Quadrilateral class. Over ride the calcPerimeter () method and implement (code) the calcPerimeter () for the Rectangle class.

4. Answer all the questions given the following class.

class Employee {
private String name;
private double pay;

Employee( ) { }
Employee(String nm) { }
Employee(String nm, double payAmt) { }
public void setName(String nm) { // set name }
public String getName( ) { // returns name }
public void setPay (double payAmt) { // set pay instance variable }
public double getPay ( ) { // return pay instance variable }
public double calcBonusPay ( ) { // calculate bonus pay (percentage of pay) }
public double calcBonusPay (double bonusPct) { // calculate bonus pay given percentage amt }
}
  • What is the name of the class?
  • Name all methods except for the constructors.
  • How many data declarations are there?
  • getPay( ) method returns what data type?
  • What constructor will be called if an object is created with the name and pay being set after object is instantiated.
  • What method(s) are overloaded?
  • Create a class called FullTime that will inherit from the Employee class.
  • Construct an Employee data type named jill with a name of "Jill"
  • Write statement(s) that will set the pay to 900 for jill object.
  • Write a method in the Employee class that will print the name and pay of the current object instance when the System.out.println(object reference) is called. EG: System.out.println(jill); // will print the following: "Jill pay is 900"

5. Use the Employee class to answer the following questions:

  • Declare an array of 20 Employee objects.
  • Assuming there are Employee instances in the array; write statements that will count the number of Employee that have a pay less than $1500.

6. Given the following interface:

public interface Speaker {
public void speak(); // print how to speak
}
  • Define and implement (code) two classes that implements interface Speaker.
  • Create an array of 10 reference the two classes defined in 6a.

7. Palindromes are words or phrases that read the same in both directions. Eg: EYE, RACECAR, MADAM IM ADAM, DONT NOD, etc. Implement the method that will test whether a given array of characters is a palindrome or not. The return type is boolean. Assume no spaces or special characters in the array of characters. Do not use java API.

public boolean isPalindrome (char[] myPalindrome) {
}

Eg: char[] myCharArray = {'R',A,C,E,C,A,R};

Coding

1. Implement the Speaker interface described below and create three classes that implement Speaker in various ways. Create a driver class whose main method instantiates some of these objects and test their abilities.

public interface Speaker {
public void speak();
public void announce( String str );

}

2. Create a program that reads a text (.txt) file and does the following:

  • Count the number of lines in the file
  • Count the number of words in the file

The text file will be read from the command prompt:

eg: (for FileScan.java program)

c:\ java FileScan myTextFile.txt
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.