These projects were created by Leigh Anne Fitz and Lisa Ferran to to provide engaging, standards-aligned learning experiences for AP Computer Science A students.
With the fundamentals of Java syntax and variables behind you, itβs time to begin working with the building blocks of object-oriented programming. In these projects, youβll explore Javaβs built-in classes, write and call methods, and manipulate String objects.
Each project is designed to reinforce concepts through authentic programming tasks. As you complete them, focus on writing clean, readable code, testing your programs carefully, and thinking about how objects and methods work together to solve problems.
Subsection1.28.1Method Headers and Method Calls Review
A method header defines a method by specifying its access modifier, return type, method name, and any parameters it accepts. The header tells Java what the method is called, what type of value it returns (if any), and what information it needs to perform its task.
public static double perimeter(double l, double w)
Think about whether this method is supposed to print the perimeter or return it. Which return type should a method use when it only displays output?
public static void perimeter(double l, double w)
The method should be void because it prints the perimeter rather than returning it. It must also accept the length and width as parameters so it can calculate the perimeter.
public static void perimeter(double p)
What information does the method need to calculate the perimeter? Can it compute the perimeter from only one parameter?
public static void perimeter(double w, double l, double p)
Ask yourself which values should be inputs to the method. Should the perimeter be passed in, or should it be calculated inside the method?
Think about whether this method should return the area or simply perform an action. What return type is needed if the method produces a value?
public static double area(double area)
Should the method be given the answer, or should it calculate the answer? Consider which values are needed to compute the area.
public static double area(double l, double w)
The method should return the calculated area, so its return type should be double. Since the length and width are double values, their product may also be a double. Returning an int could lose the decimal portion of the result.
public static int area(double l, double w)
The parameters are correct, but think about the type of value that could result from multiplying two double values. Can every possible result be stored in an int without losing information?
Can a method with a void return type be assigned to a variable?
perimeter(8.5, 4.0);
double a = area(8.5, 4.0);
The perimeter method has a void return type, so it is called as a standalone statement. The area method returns a double, so its return value should be used in some way, such as storing it in a variable.
perimeter(8.5);
double a = area(8.5, 4.0);
Compare the number of arguments in the method call to the number of parameters in the method header.
perimeter(8.5, 4.0);
area(8.5, 4.0);
The method call itself is valid, but think about what happens to the value returned by area(). Is it being used?
In the main method, declare appropriate variables for length, width, and the rectangleβs area (do not initialize them). These should allow for decimal values. Create a scanner object, and prompt the user to enter values for length and width, storing those values into the appropriate variables.
Complete the body of the perimeter method so that it calculates and prints the perimeter of the rectangle (make sure to add words to the print statement so the output shows that the value represents the perimeter).
Back in main, call the area method, sending in the appropriate variables, and store the result of the call into your area variable. Still in main, print the area of the rectangle (make sure to add words to the print statement so the output shows that the value represents the area).
Write a program that calculates and displays the perimeter and area of a rectangle using double amounts for the length and width provided by the user. See above for more detailed instructions.
A static method belongs to the class, not to an individual object. Because of this, you do not need to create an object before calling a static method.
Since this call is being made from another class, what must be included before the method name?
double area = Geometry.findArea( );
Compare the number of arguments in the method call to the number of parameters in the method header.
int area = Geometry.findArea(8, 12);
What type of value does the method return?
double area = Geometry.findArea(8.5, 4.0);
Because findArea is a static method in the Geometry class, it is called using the class name, a dot (.), and the method name. The method requires two arguments (length and width) and returns a double, which is stored in the variable area.
Write a Java program that calculates and displays the area and circumference of a circle and the volume and surface area of a sphere. You will be writing two classes for this program. One will use the RoundThingsDriver class and the other will use the RoundThings class.
Processing: In main, declare all variables listed above and create a Scanner object. Prompt the user to enter a value for the radius and store that value into the appropriate variable. Call the methods you wrote in RoundThings and store the results into the appropriate variables.
Method:outPut - This method will receive 4 values as parameters: a description of the type of value calculated (i.e. circumference, area, volume, surface area), one for the shape name (i.e. circle or sphere), one for the radius inputted by the user, and one for the actual value calculated. It will display (print) the information formatted nicely (see below).
The area of a circle with a radius of # is #.
The volume of a sphere with a radius of # is #.β
(# will be replaced by the appropriate value)
In main, call the outPut method four times. The first time should be for the area of a circle. The second time should be for the circumference of a circle. The third time should be for the surface area of a sphere. The fourth time should be for the volume of a sphere. They should all use the same radius value.
Write a program that reads in a single String from the user containing exactly four words separated by symbols with no spaces into a single String object. Next, extract each word from the original string and store each word in a String object. Then, concatenate the words in reverse order to form another string. Display both the original and final strings.
Hint: to extract the words, you should use the indexOf ( ) method to find each symbol and then use the substring ( ) method to get the characters between the * symbols.