Java Programming Test: Circle Class & Digit Sum Exercise
Test 1
Programming I
Monday, September 29, 2008
Name: ____________________________________________________________
Part A: Circle Class Implementation
Given the Circle
class defined with the following attributes and method signatures:
Circle Class Definition
public class Circle {
private int radius;
private int x, y;
// Constructor with parameters
public Circle(int x, int y, int radius) { /* implementation */ }
// Default constructor (initializes with zeros)
public Circle() { /* implementation */ }
// Static method to calculate area
public static double area(Circle c) { /* implementation */ }
// Method to represent Circle as a String
public String toString() { /* implementation */ }
// Getter methods
public int getX() { /* implementation */ }
public int getY() { /* implementation */ }
public int getRadius() { /* implementation */ }
// Setter methods
public void setX(int x1) { /* implementation */ }
public void setY(int y1) { /* implementation */ }
public void setRadius(int r1) { /* implementation */ }
// Method to increase radius by a percentage
public void grow(int percentage) { /* implementation */ }
}
Tasks
Using the Circle
class definition above, solve the following tasks:
- Create two objects of type
Circle
:c1
using the first constructor with random integer values between 1 and 10 (inclusive) for x, y, and radius.c2
using the second (default) constructor.
- Calculate the area of
c1
using the staticarea
method. - Calculate the perimeter of
c1
. Remember that the perimeter (circumference) is 2 * 3.14159 * radius. - Display the attributes of
c1
on the screen using itstoString
method. - Increase the radius of
c1
by an integer value read from the keyboard. - Increase the radius of
c1
by 10% using thegrow
method. - (For the student) Illustrate the objects (
c1
,c2
) and their attributes, showing how they change. Also, show the variables (area
,perimeter
,keyboard
) and their values. Include a depiction of the screen output with correct line breaks.
Sample Solution Code (Part A)
Note: Corrections applied for syntax and clarity. Assumes necessary imports like java.util.Scanner
and java.util.Random
or Math.random()
usage. Point values retained for context.
import java.util.Scanner;
public class CircleTest {
public static void main(String[] args) throws Exception {
Scanner read = new Scanner(System.in);
// Task 1: Create Circle objects
// Random values between 1 and 10
int randX = (int)(Math.random() * 10) + 1;
int randY = (int)(Math.random() * 10) + 1;
int randRadius = (int)(Math.random() * 10) + 1;
Circle c1 = new Circle(randX, randY, randRadius); // 3 pts
Circle c2 = new Circle(); // 1 pt
double area, perimeter;
int keyboardInput;
// Task 2: Calculate area
area = Circle.area(c1); // 3 pts
// Task 3: Calculate perimeter
perimeter = 2 * 3.14159 * c1.getRadius(); // 3 points
// Task 4: Show c1 attributes
System.out.println(c1); // 1 point (Assumes toString() is implemented appropriately)
// Task 5: Increase radius from keyboard input
System.out.println("Enter value to add to radius = ");
keyboardInput = read.nextInt(); // 1 point
c1.setRadius(c1.getRadius() + keyboardInput); // 4 points
// Task 6: Increase radius by 10%
c1.grow(10); // 2 points (Assuming 'aumentar' meant 'grow')
// Display final state or intermediate values if needed for debugging/verification
System.out.println("After modifications: " + c1);
System.out.println("Calculated Area: " + area);
System.out.println("Calculated Perimeter: " + perimeter);
read.close(); // Good practice to close the scanner
}
}
Variable Tracking Example
(Illustrative example based on the structure in the original text, assuming initial random values r1, x1, y1 for c1)
c1
:x
: Initiallyx1
, remainsx1
y
: Initiallyy1
, remainsy1
radius
: Initiallyr1
. Changes tor1 + keyboardInput
. Changes again to(r1 + keyboardInput) * 1.10
(aftergrow(10)
).
c2
:x
: 0y
: 0radius
: 0
keyboardInput
: Stores the integer value read from input.area
: Stores3.14159 * r1 * r1
(calculated using the initialr1
).perimeter
: Stores2 * 3.14159 * r1
(calculated using the initialr1
).
(Point values like 0.4, 0.3, Total 3.0 points from the original seem related to grading specific steps/variables and are kept for context where they appeared).
Expected Screen Output
(Based on the sample code execution flow)
x=x1, y=y1, radius=r1 (Output from the first System.out.println(c1), assuming a suitable toString() format) Enter value to add to radius = (User enters a value, e.g., 5) After modifications: x=x1, y=y1, radius=new_radius (Output from the second System.out.println(c1)) Calculated Area: calculated_area Calculated Perimeter: calculated_perimeter
(Original point values: 0.4 pts. Sum probe 1: 21 points – likely grading notes)
Part B: Sum of Digits Calculation
Problem Description
Calculate the sum of the digits of a five-digit integer read from the keyboard.
Examples:
- Input:
13512
-> Output:12
(1+3+5+1+2) - Input:
11111
-> Output:5
(1+1+1+1+1)
Input and Output
- Input: A five-digit positive integer. (1 pt)
- Output: An integer representing the sum of the input’s digits. (1 pt)
Numerical Example Walkthrough
Let the input number n = 13512
. Initialize sum = 0
.
sum = sum + (n % 10)
=>sum = 0 + (13512 % 10)
=>sum = 0 + 2
=>sum = 2
. (0.2 pts)n = n / 10
=>n = 13512 / 10
=>n = 1351
. (0.33 pts)sum = sum + (n % 10)
=>sum = 2 + (1351 % 10)
=>sum = 2 + 1
=>sum = 3
. (0.2 pts)n = n / 10
=>n = 1351 / 10
=>n = 135
. (0.33 pts)sum = sum + (n % 10)
=>sum = 3 + (135 % 10)
=>sum = 3 + 5
=>sum = 8
. (0.2 pts)n = n / 10
=>n = 135 / 10
=>n = 13
. (0.33 pts)sum = sum + (n % 10)
=>sum = 8 + (13 % 10)
=>sum = 8 + 3
=>sum = 11
. (0.2 pts)n = n / 10
=>n = 13 / 10
=>n = 1
. (Original text had sum calculation here, corrected to update n)sum = sum + (n % 10)
=>sum = 11 + (1 % 10)
=>sum = 11 + 1
=>sum = 12
. (0.2 pts)n = n / 10
=>n = 1 / 10
=>n = 0
. The loop condition (n > 0
) would now be false.
Final sum = 12
. (Original text had sum = sum + 13/10 // sum total 5 = 12 0.2, sum = 1 – these seem like calculation errors or typos in the original walkthrough).
Sample Solution Code (Part B)
Note: Corrections applied for clarity, variable initialization, and using a loop for robustness. Point values retained for context.
import java.util.Scanner;
public class DigitSumCalculator { // Renamed from Programa2 (0.2 pts)
public static void main(String[] args) throws Exception { // (0.2 pts)
Scanner read = new Scanner(System.in); // (0.2 pts)
int sum = 0; // (0.4 pts for sum, value declaration)
int number; // Renamed from 'value' and 'n' for clarity
int workingCopy; // To preserve the original number if needed
System.out.println("Enter a five-digit integer:"); // Prompt clarifies input
number = read.nextInt(); // Read input (1 pt for reading, example value 13512)
// Ensure it's a positive number for this logic
if (number < 0) {
System.out.println("Please enter a positive integer.");
} else {
workingCopy = number; // Work with a copy
// Loop to sum digits
while (workingCopy > 0) {
sum = sum + (workingCopy % 10); // Add the last digit (1 pt per step in original)
workingCopy = workingCopy / 10; // Remove the last digit (1 pt per step in original)
}
/* // Original fixed-step logic (less robust than a loop)
int n = number; // Initialize n
sum = n % 10; // 1 pt
n = n / 10; // 1 pt
sum = sum + n % 10; // 1 pt
n = n / 10; // 1 pt
sum = sum + n % 10; // 1 pt
n = n / 10; // 1 pt
sum = sum + n % 10; // 1 pt
n = n / 10; // 1 pt (Now n is the most significant digit)
sum = sum + n; // Add the last remaining digit (1 pt + 1 pt = 2 pts in original)
*/
System.out.println("The sum of the digits is = " + sum); // (1 pt)
}
read.close(); // Good practice
} // (End main: points likely cumulative, e.g., PROG2 18 total)
}