014-008-002

Object Inheritance: toString() Override

Easy

Problem Description

Object inheritance: toString() override

In this problem, you will create a program that reads product data via Scanner, creates a Product class with id, name, and price fields, and overrides the toString() method to format product information in a specified pattern.

Learning Objective: Override Object class method to customize object's string representation

Read product data with Scanner, create Product objects, and override toString() method to display product information clearly.

Input

Line 1: number of products n
Then for each of n products:

  • id (string)
  • name (string)
  • price (integer)

Output

===== Product Information =====
Product{id='...', name='...', price=...}
...
==============================

Examples

Example 1: Display 2 products

Input:

2
P001
Laptop
89800
P002
Mouse
2980

Output:

===== Product Information =====
Product{id='P001', name='Laptop', price=89800}
Product{id='P002', name='Mouse', price=2980}
==============================

Example 2: Display 1 product

Input:

1
X001
Tablet
59800

Output:

===== Product Information =====
Product{id='X001', name='Tablet', price=59800}
==============================

Automatic toString() Invocation

  • System.out.println(product) automatically calls toString()
  • String concatenation "Info: " + product also calls it
  • String.valueOf(product) also calls it.

Test Cases

※ Output examples follow programming industry standards

Normal case
Input:
2
E001
Smartphone
78000
E002
Camera
45000
Expected Output:
===== Product Information =====
Product{id='E001', name='Smartphone', price=78000}
Product{id='E002', name='Camera', price=45000}
==============================
Normal case
Input:
2
A100
Keyboard
5980
B200
Monitor
32000
Expected Output:
===== Product Information =====
Product{id='A100', name='Keyboard', price=5980}
Product{id='B200', name='Monitor', price=32000}
==============================

Your Solution

Current Mode: My Code
Product.java🔒
Solution.java🔒
2/6 ファイル289B
public class Product {
}
0 B / 5 MB

You have 10 free executions remaining