Class with String Fields: Business Card Manager
Problem Description
[Explanation]
Input Specification
Read the following 4 lines from standard input:
- Line 1: Name (String)
- Line 2: Company name (String)
- Line 3: Job title (String)
- Line 4: Email address (String)
Output Specification
Print the string returned by getCardInfo() to standard output.
1. Problem Overview
This problem teaches the basics of classes that handle strings. By creating a class to manage business card information used in business settings, you can practically understand "what is a class", "what are fields", and "the roles of constructors and methods". Similar designs are used in actual business card management applications, making this a practical skill for real work.
2. Prerequisites
What is a class?
A class is a "blueprint for objects". When you want to manage business card information, a class defines what information is needed (name, company, etc.) and what operations are possible (retrieve information, display, etc.).
Reading Input with Scanner
Use the Scanner class to read strings from standard input. Use scanner.nextLine() to read one line at a time. Pass the read values to the NameCard constructor to create an object.
What are Fields?
Fields are "variables that store data" within a class. In a business card class, the name field stores a string like "Alice Johnson". Adding private prevents direct modification from outside the class, protecting the data.
What is a constructor?
A constructor is a "special method automatically called when creating an object". When you write new NameCard("Alice", "TechCorp", ...), this constructor executes and the passed arguments are stored in fields.
The this Keyword
In this.name = name;, this means "this object's". The left side this.name refers to the field, while the right side name refers to the argument. When using the same name, use this to distinguish them.
3. Program Structure
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
String company = scanner.nextLine();
String title = scanner.nextLine();
String email = scanner.nextLine();
scanner.close();
NameCard card = new NameCard(name, company, title, email);
System.out.println(card.getCardInfo());
}
}
4. Common Mistakes
Mistake 1: Forgetting the this Keyword
// ❌ Wrong
public NameCard(String name, String company, String title, String email) {
name = name; // Assigns to argument instead of field
}
// ✅ Correct
public NameCard(String name, String company, String title, String email) {
this.name = name; // Specifies field with this
}
Mistake 2: Wrong Return Type for Getter Method
// ❌ Wrong
public void getName() {
return name; // Error: void means nothing is returned
}
// ✅ Correct
public String getName() {
return name;
}
5. Debugging Tips
If You Get Compile Errors
- Check if class name matches file name (Main.java)
- Check if all methods have appropriate return types
- Check if constructor name exactly matches class name
If You Don't Get Expected Output
- Use System.out.println() to check return values of each method
- Check if field values are correctly initialized
- Check if string concatenation is done correctly
6. Related Learning Topics
- Details of encapsulation (information hiding) (Category 014)
- Basics of object-oriented programming (Category 023)
- Collections (managing multiple business cards) (Category 015)
Test Cases
※ Output examples follow programming industry standards
Alice Johnson TechCorp Engineer alice@techcorp.com
+----+ | Name: Alice Johnson | | Company: TechCorp | | Title: Engineer | | Email: alice@techcorp.com | +----+
Bob Smith InnovateLabs Manager bob@innovate.com
+----+ | Name: Bob Smith | | Company: InnovateLabs | | Title: Manager | | Email: bob@innovate.com | +----+
Your Solution
- No main method found
You have 10 free executions remaining
