009-001-002

PointCard Class: Point Management with Public Fields and Methods

Medium

Problem Description

[Explanation]

In this problem, you will implement the PointCard class that manages object state through fields such as points.

1. Problem Overview and Purpose

This problem teaches about the "public modifier", one of the most fundamental access modifiers in Java. Public means "open" and indicates that fields or methods can be freely accessed from outside the class.

In real-world applications, like point card systems, there are many situations where you need to change state or retrieve information from outside. By correctly understanding the public modifier, you'll be able to properly design data exchange between classes.

This learning forms the foundation for concepts like "private modifier" and "encapsulation" that you'll learn next. First, understand access control mechanisms with public, then move on to learning safer design methods.

2. Prerequisites

Class and Field Basics

A class is a blueprint for objects. For example, the "PointCard" class defines the mechanism of a point card.

A field is a variable that stores data held by a class. In this problem, the field public int points stores the current number of points. Like variables, it can hold values, but differs in being defined as part of a class.

Role of Methods

A method defines the "functionality" or "behavior" of a class. This problem implements two methods: addPoints and getPoints.

  • addPoints: Functionality to add points
  • getPoints: Functionality to retrieve current points

What is the Public Modifier

public is a type of access modifier. Access modifiers control "who can access this field or method".

When you add public, that member can be accessed from anywhere (even from other classes). This allows external code to manipulate the point card.

3. Code Explanation Step by Step

Class Declaration

public class PointCard {
```java
By making the class itself public, this class can be used from other files.

### Field Declaration
```java
public int points = 0;
```java
This line does the following:
1. The `public` modifier allows direct access to this field from outside the class
2. `int` type allows storing integer values (point count)
3. Named `points`
4. `= 0` sets initial value to 0 (points start from zero)

### addPoints Method
```java
public void addPoints(int amount) {
```java
- `public`: Allows external calls
- `void`: This method doesn't return a value (no return value)
- `addPoints`: Method name
- `(int amount)`: Receives the number of points to add as an argument

```java
if (amount > 0) {
    points += amount;
}
```java
This part is important:
1. `if (amount > 0)` checks if the received value is positive
2. Only if positive, `points += amount` adds the points
3. This prevents points from becoming negative by adding negative values

### getPoints Method
```java
public int getPoints() {
    return points;
}
```java
- `public`: Can be called externally
- `int`: Indicates it returns an integer value
- `return points`: Returns current point count to the caller

By calling this method, you can check the current point balance from outside.

## 4. Common Mistakes and Corrections

### Mistake 1: Forgetting public

**Incorrect code:**
```java
int points = 0;
void addPoints(int amount) { ... }
```java

**Reason:** Omitting the access modifier makes it "package-private" (accessible only from within the same package). This problem requires explicit external access permission, so public is necessary.

**Correct code:**
```java
public int points = 0;
public void addPoints(int amount) { ... }
```java

### Mistake 2: Forgetting input validation

**Incorrect code:**
```java
public void addPoints(int amount) {
    points += amount;  // Also adds negative values
}
```java

**Reason:** When a negative value is passed, points become negative. This is an impossible state in a real point card system.

**Correct code:**
```java
public void addPoints(int amount) {
    if (amount > 0) {  // Only add positive values
        points += amount;
    }
}
```java

### Mistake 3: Returning wrong value from getPoints

**Incorrect code:**
```java
public int getPoints() {
    return 0;  // Always returns 0
}
```java

**Reason:** Returns 0 instead of the actual point balance (points).

**Correct code:**
```java
public int getPoints() {
    return points;  // Correctly returns the field value
}
```java

## 5. Practical Debugging Tips

### Error: cannot find symbol
```java
error: cannot find symbol
  symbol:   method getPoints()
```java
If this error appears, there may be a spelling mistake in the method name or you may have forgotten to add public. Check the method declaration.

### Error: incompatible types
```java
error: incompatible types: void cannot be converted to int
```java
This error occurs when the return type is incorrect.

Test Cases

※ Output examples follow programming industry standards

Input:
Expected Output:
120
Input:
Expected Output:
250
Input:
Expected Output:
50
Input:
Expected Output:
100
Input:
Expected Output:
0
❌ Some tests failed
❌ エラー発生

Your Solution

Current Mode: My Code
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Write your code here

sc.close();
}
}
0 B / 5 MB

You have 9 free executions remaining