Skip to main content

Section 1.28 Unit 1B Projects

90 minutes
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.

Subsection 1.28.1 Method 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 int add(int a, int b)
A method call is used to execute a method. When calling a method, provide any required arguments in the correct order and type.
int sum = add(4, 7);
Remember:
  • The method header defines the method.
  • The method call uses (invokes) the method.
  • The arguments in a method call must match the parameters in the method header.
  • If a method returns a value, you can store it in a variable, use it in an expression, or print it.

Activity 1.28.1.

Which of the following is the correct method header for a method that prints the perimeter of a rectangle given the length and width?
  • 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?

Activity 1.28.2.

Which of the following is the correct method header for a method that returns the area of a rectangle given the length and width?
  • public static void area(double l, double w)
  • 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?

Activity 1.28.3.

Using the two correct method headers from the previous problems, which of the following correctly call both methods?
  • double p = perimeter(8.5, 4.0);
    double a = area(8.5, 4.0);
  • 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?

Subsection 1.28.2 Unit 1B Project 1 - My Shape

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.
  1. 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.
  2. 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).
  3. Back in main, call the perimeter method, sending in the appropriate variables.
  4. Complete the body of the area method so that it calculates and returns the value of the area of the rectangle.
  5. 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).
  6. Test your program to make sure both area and perimeter are calculating and printing correctly.

Activity 1.28.4.

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.

Subsection 1.28.3 Static Methods Review

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.
To call a static method in the same class, simply use its name: printGreeting( );
To call a static method from another class, use the class name followed by a dot (.): Math.sqrt(25);
You will find the name of the class in the class header: public class Math
Remember:
  • Static methods belong to the class.
  • You can call a static method without creating an object.
  • If the method is in another class, use ClassName.methodName(...)
  • Provide the required arguments in the correct order and type when calling the method.

Activity 1.28.5.

The following method is defined in a class named Geometry.
public class Geometry
{
    public static double findArea(double length, double width)
    {
        return length * width;
    }
}
Which of the following correctly calls the findArea method from another class and stores the result in a variable?
  • double area = findArea(8.5, 4.0);
  • 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.

Subsection 1.28.4 Unit 1B Project 2 - Round Things

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.
Class Name: RoundThings
There will not be a main method in this class. This class will just have four methods.
Methods
All these methods will be static. Use Math.PI for pi. The radius parameter will be a double value.
  1. calcAreaCircle() - receives the radius of a circle, and returns the area
  2. calcCircumCircle() - receives the radius of a circle and returns the circumference
  3. calcAreaSphere() - receives the radius of a Sphere and returns the area
  4. calcVolumeSphere() - receives the radius of a Sphere and returns the volume - watch out for integer division!
Class Name: RoundThingsDriver
This will have a main method and will call on methods from RoundThings.
Variables:
  1. radius - input variable, a double value (to be gotten from the user using a Scanner)
  2. area - a double value returned from the calcAreaCircle method
  3. circumference - a double value returned from the calcCircumCircle method
  4. surfaceArea - a double value returned from the calcAreaSphere method
  5. volume - a double value returned from the calcVolumeSphere method
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.

Activity 1.28.6.

Subsection 1.28.5 String Methods Review

The String class provides many useful methods for working with text. Two of the most common are indexOf() and substring().
The indexOf() method returns the index of the first occurrence of a character or string. If it is not found, the method returns -1.
String word = "computer";
int index = word.indexOf("p"); // index is 3
One version of substring() returns a new string starting at a beginning index and continuing to the end of the original string.
String word = "computer";
String part = word.substring(3); // "puter"
A second version of substring returns the characters from a beginning index up to, but not including, an ending index.
String word = "computer";
String part = word.substring(3, 6); // "put"
Remember:
  • String indexing begins at 0.
  • indexOf() returns the first matching index or -1 if not found.
  • substring(beginIndex) goes to the end of the string.
  • substring(beginIndex, endIndex) does not include the character at endIndex.

Subsection 1.28.6 Unit 1B Project 3 - String Program

StringProgram
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.
Example output:
Original: one*two*three*four
Reverse: four three two one

Activity 1.28.7.

You have attempted of activities on this page.