007-001-013
Class Creation: Constructor
Medium
Problem Description
class Creation: constructor
In this problem, you will define a Product class, initialize name and price fields using a constructor, and implement a getInfo() method that returns product information.
Learning Objective: Understand how to initialize fields using constructors
Overview
Create a class that initializes fields using a constructor.
Specifications
- Create a
Productclass - Define a String field
name - Define an int field
price - Initialize both fields in constructor
- Create
getInfo()method that returns "name: price yen" format string
Behavior Examples
Product p = new Product("Apple", 150);
System.out.println(p.getInfo()); // Apple: 150 yen
Product q = new Product("Banana", 200);
System.out.println(q.getInfo()); // Banana: 200 yen
Ready to Try Running Code?
Log in to access the code editor and execute your solutions for this problem.
Don't have an account?
Sign Up