Java Hello Tangibles
Completing the Java Hello Hacks.
// Class declaration
public class Car {
// Fields (also known as instance variables)
private String make; // The make of the car (e.g., Toyota)
private String model; // The model of the car (e.g., Camry)
private int year; // The year the car was manufactured (e.g., 2022)
private double price; // The price of the car in dollars (e.g., 25000.00)
// Definition of a Class: This is where we define the 'Car' class.
// Constructor with parameter differences:
// The constructor below takes parameters to initialize the object's fields.
public Car(String make, String model, int year, double price) {
this.make = make;
this.model = model;
this.year = year;
this.price = price;
}
// Methods (functions associated with the class)
// Getter method for 'make' field
public String getMake() {
return make;
}
// Getter method for 'model' field
public String getModel() {
return model;
}
// Getter method for 'year' field
public int getYear() {
return year;
}
// Getter method for 'price' field
public double getPrice() {
return price;
}
// Setter method for 'price' field
// Calling an object method with a parameter (setter)
public void setPrice(double price) {
this.price = price;
}
// A custom method to display car information
public void displayInfo() {
System.out.println("Make: " + make);
System.out.println("Model: " + model);
System.out.println("Year: " + year);
System.out.println("Price: $" + price);
}
}
// Instance of a Class (Object)
public class Main {
public static void main(String[] args) {
// Creating an instance (object) of the 'Car' class
Car myCar = new Car("Toyota", "Camry", 2022, 25000.00);
// Calling the 'displayInfo' method to display car information
myCar.displayInfo();
// Calling the 'setPrice' method to change the car's price
myCar.setPrice(27000.00);
// Displaying updated car information
myCar.displayInfo();
}
}
Car |
---|
-------------------------------------------------------Attributes------------------------------------------------------ |
make : String |
model : String |
year : int |
price : double |
---------------------------------------------------------Methods------------------------------------------------------ |
getMake(): String |
getModel(): String |
getYear(): int |
getPrice(): double |
setPrice(price: double): void |
displayInfo(): void |
The main portions of the class are the attributes within the Car class which are the make
which is a string, model
which is a string, year
which is an integer, and price
which is a double. The methods within the class are the getMake()
which returns the make of the car, getModel()
which returns the model of the car, getYear()
which returns the year of the car, getPrice()
which returns the price of the car, setPrice()
which sets the price of the car, and displayInfo()
which displays the information of the car.
// Person class represents a person with name and age attributes.
public class Person {
private String name; // The name of the person.
private int age; // The age of the person.
// Constructor to initialize the person object with a name and age.
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// Getter method to retrieve the person's name.
public String getName() {
return name;
}
// Getter method to retrieve the person's age.
public int getAge() {
return age;
}
// Setter method to change the person's name.
public void setName(String name) {
this.name = name;
}
// Setter method to change the person's age.
public void setAge(int age) {
this.age = age;
}
}
// Dessert class represents a dessert with type and cost attributes.
public class Dessert {
private String type; // The type of dessert.
private double cost; // The cost of the dessert.
// Constructor to initialize the dessert object with a type and cost.
public Dessert(String type, double cost) {
this.type = type;
this.cost = cost;
}
// Getter method to retrieve the dessert's type.
public String getType() {
return type;
}
// Getter method to retrieve the dessert's cost.
public double getCost() {
return cost;
}
// Setter method to change the dessert's type.
public void setType(String type) {
this.type = type;
}
// Setter method to change the dessert's cost.
public void setCost(double cost) {
this.cost = cost;
}
}
// Location class represents a location with city, state, and zip attributes.
public class Location {
private String city; // The city of the location.
private String state; // The state of the location.
private String zip; // The zip code of the location.
// Constructor to initialize the location object with city, state, and zip.
public Location(String city, String state, String zip) {
this.city = city;
this.state = state;
this.zip = zip;
}
// Getter method to retrieve the location's city.
public String getCity() {
return city;
}
// Getter method to retrieve the location's state.
public String getState() {
return state;
}
// Getter method to retrieve the location's zip code.
public String getZip() {
return zip;
}
// Setter method to change the location's city.
public void setCity(String city) {
this.city = city;
}
// Setter method to change the location's state.
public void setState(String state) {
this.state = state;
}
// Setter method to change the location's zip code.
public void setZip(String zip) {
this.zip = zip;
}
}
public class ClassTester {
public static void main(String[] args) {
// Creating instances of the Person class
Person person1 = new Person("Alice", 30); // Creating a Person object with name "Alice" and age 30
Person person2 = new Person("Bob", 25); // Creating another Person object with name "Bob" and age 25
// Creating instances of the Dessert class
Dessert dessert1 = new Dessert("Cake", 12.99); // Creating a Dessert object with type "Cake" and cost $12.99
Dessert dessert2 = new Dessert("Ice Cream", 5.49); // Creating another Dessert object with type "Ice Cream" and cost $5.49
// Creating instances of the Location class
Location location1 = new Location("New York", "NY", "10001"); // Creating a Location object for New York
Location location2 = new Location("Los Angeles", "CA", "90001"); // Creating a Location object for Los Angeles
// Displaying initial data
System.out.println("Initial Data:");
displayInfo(person1, dessert1, location1); // Displaying information for person1, dessert1, and location1
displayInfo(person2, dessert2, location2); // Displaying information for person2, dessert2, and location2
// Using setter methods to change data dynamically
person1.setAge(31); // Changing the age of person1 using a setter
dessert2.setCost(6.49); // Changing the cost of dessert2 using a setter
location1.setZip("10002"); // Changing the zip code of location1 using a setter
// Displaying updated data
System.out.println("\nUpdated Data:");
displayInfo(person1, dessert2, location1); // Displaying updated information for person1, dessert2, and location1
}
// Helper method to display information
private static void displayInfo(Person person, Dessert dessert, Location location) {
System.out.println("Person: Name = " + person.getName() + ", Age = " + person.getAge());
System.out.println("Dessert: Type = " + dessert.getType() + ", Cost = $" + dessert.getCost());
System.out.println("Location: City = " + location.getCity() + ", State = " + location.getState() + ", Zip = " + location.getZip());
System.out.println();
}
}