Question
1. What is the difference between a static and a non-static method? 2. Suppose there is a class AreaCalc with static methods that allow you
1. What is the difference between a static and a non-static method?
2. Suppose there is a class AreaCalc with static methods that allow you to calculate the area of specific shapes. Further suppose there is a static method called triangleArea that accepts two arguments, the first being the base of a triangle, and the second being the height. Write a Java statement that calls the triangleArea method to calculate the area of a triangle with a base of 15 and a height of 32 and stores the result in a variable of type double that you declare.
3. Suppose you want to call a method that only accepts Objects for arguments and that you only have variables with a primitive data type. What would you need to do in order to be able to pass the values of those primitive variables to that method?
4. Write some Java code that will allow you to find the find the largest value of three int variables with the identifiers x, y, and z and store that value in a previously declared int variable with the identifier minValue.
5. Describe what the following Java statement does: boolean b = aString.replace(" ","").toLowerCase().endsWith("o");
6. What is the relationship between an object and a class?
7. Another programmer is having some difficulties with their code and cannot get the correct result to print out. What is wrong with their code? How would you fix it? Note: You may assume that accountBalance is correctly declared and initialized. NumberFormat n = NumberFormat.getCurrencyInstance(Locale.UK); n.format(accountBalance); System.out.println("You have a balance of " + n);
8. Write an accessor method for a String type instance variable with the identifier name. You should use typical Java conventions when doing so.
9. You have just written a class that contains 3 instance variables, a boolean, a String, and an int, but forgot to assign them values in your constructor method. What values will these instance variables have for a new object instantiated with that constructor?
10. In a momentary lapse of judgment Rick decided to let his grandson Morty write some Java code to manage his finances. However when he checked his bank account, he noticed that the notorious alien scammer Prince Nebulon completely cleaned it out. When asking Morty about it, he said Gee Rick, I dont know. I havent even written any withdraw method or anything like that yet. What is likely wrong with Mortys code that caused this problem?
11. Write a mutator method for an int type instance variable with the identifier age. You should use typical Java conventions when doing so.
12. List three different access modifiers and indicate which is the rank them in order of how restrictive they are (i.e. indicate which is the most restrictive, and which is the least restrictive or most open).
13. Override the toString method for the Mogthwamp class. This class has two instance variables a String type with the identifier mog and an int type with the identifier thwamp.
14. Write a default constructor method for the Employee class. You can assume that the class has two instance variables a String type with the identifier name and an int type with the identifier ID. You may assign values of your choosing so long as they are of the appropriate type.
15. Other than using a constructor method to create an instance of a class, what is one other method (approach) that can be used to get an object?
16. You have the following Java code thats part of a class you have written, but you are unable to compile it. What is the likely cause of the problem? You may assume that bunnies is an instance variable. public static int getFuturePopulation(int growthFactor) { return (int)Math.pow(bunnies, growthFactor); }
17. Write a factory method for the Cycle class that will return a Cycle object with the name "tricycle" and 3 wheels. You can assume that the constructor method for the Cycle class takes a String and an int as arguments and these correspond to the name and number of wheels for a Cycle.
18. What is wrong with the following code example:
public class Person
{ private String name;
public void setName(String name) {
name = name; }
}
19. Suppose you are creating your own Java class to model a City and want to store information about how much rainfall a city receives in an average year. A) Propose a data type and identifier for that field. B) Indicate why you believe your choice of data type is reasonable or better than alternatives.
20. Write a static method that will convert feet to inches. Your method should be accessible to other classes, take in a single parameter of type int which represents the number of feet to convert, and return a value of type int which the number of inches contained in that parameter. There are 12 inches in 1 foot for the purpose of doing the conversion.
21. What are the four parts of a method signature?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started