Question

IN JAVA PLEASE Lab 28.1 Add a class named Purchase with: an instance (String) variable for...

IN JAVA PLEASE

Lab 28.1 Add a class named Purchase with: an instance (String) variable for the customer's name an instance (double) variable for the customer's balance a member method public String toString() that returns the object's instance variable data as a single String; e.g., Cathy has a balance of $100.00 Lab 28.2 Add a main class named Store;

. Then, add code that: declares and creates an ArrayList of Purchase objects named purchases (make sure to add import java.util.ArrayList at the top of your source code) creates and adds Purchase objects to the purchases list for each of the following: a customer named Cathy with a balance of $100.00 a customer named Ben with a balance of $234.56 a customer named Jorge with a balance of $2.49 a customer named Wanda with a balance of $32.32 a customer named Freddie with a balance of $400.00 prints out the final purchases list (hint: you can use a single call to System.out.println() with the purchases list)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/* Purchase.java */

public class Purchase {
// instance variable
private String name;
private double balance;
// constructor
public Purchase(String name, double balance) {
this.name = name;
this.balance = balance;
}
// to string method
@Override
public String toString() {
return name + " has a balance of $" + balance;
}
  
}

/* Store.java */

import java.util.ArrayList;

public class Store {

public static void main(String[] args) {
// arraylist
ArrayList<Purchase> purchases = new ArrayList<>();
// add object
purchases.add(new Purchase("Cathy",100.00));
purchases.add(new Purchase("Ben",234.56));
purchases.add(new Purchase("Freddie",400.00));
// print list
for (Purchase object:purchases) {
System.out.println(object.toString());
}
}
}

/* OUTPUT */

/* PLEASE UPVOTE */

Add a comment
Know the answer?
Add Answer to:
IN JAVA PLEASE Lab 28.1 Add a class named Purchase with: an instance (String) variable for...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java Write a class named CheckingAccount containing: An instance variable named balance of type double, initialized...

    Java Write a class named CheckingAccount containing: An instance variable named balance of type double, initialized to 0. A method named deposit that accepts a parameter of type double. The value of the balance instance variable is increased by the value of the parameter. A method named withdraw that accepts a parameter of type double. The value of the balance instance variable is decreased by the value of the parameter. A method named getBalance that accepts no parameters, returns the...

  • Define a class named Payment that contains an instance variable "paymentAmount" (non-static member variable) of type...

    Define a class named Payment that contains an instance variable "paymentAmount" (non-static member variable) of type double that stores the amount of the payment and appropriate accessor (getPaymentAmount() ) and mutator methods. Also create a method named paymentDetails that outputs an English sentence to describe the amount of the payment. Override toString() method to call the paymentDetails() method to print the contents of payment amount and any other details not included in paymentDetails(). Define a class named CashPayment that is...

  • Modern language elements Write a string store class to manage string value in a member variable named        string. The variable’s value should be setable and retrievable using a property named Data....

    Modern language elements Write a string store class to manage string value in a member variable named        string. The variable’s value should be setable and retrievable using a property named Data. The string store class must also publish an event which is emitted if the data field’s value is changed to null. The event must provide the number of times the value was changed to null (including the current case) as its parameter. The event should not be emitted if...

  • Write a Java class named Employee to meet the requirements described in the UML Class Diagram...

    Write a Java class named Employee to meet the requirements described in the UML Class Diagram below: Note: Code added in the toString cell are not usually included in a regular UML class diagram. This was added so you can understand what I expect from you. If your code compiles cleanly, is defined correctly and functions exactly as required, the expected output of your program will solve the following problem: “An IT company with four departments and a staff strength...

  • can you solve it in java please Create the following: 1. Class Invoice ( the node...

    can you solve it in java please Create the following: 1. Class Invoice ( the node ) that includes three instance variables:     int No; // the Invoice No             String CustName; // the Customer name             int Amount; // the Invoice Amount Invoice next; // points to the next Invoice Default and overloaded constructors 2. Class Shop that includes three instance variables: Invoice head; Invoice Tail; Your class should have the following: • A method that initializes the instance variables....

  • java This lab is intended to give you practice creating a class with a constructor method,...

    java This lab is intended to give you practice creating a class with a constructor method, accessor methods, mutator methods, equals method , toString method and a equals method. In this lab you need to create two separate classes, one Student class and other Lab10 class. You need to define your instance variables, accessor methods, mutator methods, constructor, toString method and equals method in Student class. You need to create objects in Lab10 class which will have your main method...

  • Java code for the following inheritance hierarchy figure.. 1. Create class Point, with two private instance...

    Java code for the following inheritance hierarchy figure.. 1. Create class Point, with two private instance variables x and y that represented for the coordinates for a point. Provide constructor for initialising two instance variables. Provide set and get methods for each instance variable, Provide toString method to return formatted string for a point coordinates. 2. Create class Circle, its inheritance from Point. Provide a integer private radius instance variable. Provide constructor to initialise the center coordinates and radius for...

  • This is a java program that runs on the Eclipse. Java Programming 2-1: Java Class Design...

    This is a java program that runs on the Eclipse. Java Programming 2-1: Java Class Design Interfaces Practice Activities Lesson objectives: Model business problems using Java classes Make classes immutable User Interfaces Vocabulary: Identify the vocabulary word for each definition below. A specialized method that creates an instance of a class. A keyword that qualifies a variable as a constant and prevents a method from being overridden in a subclass. A class that it can't be overridden by a subclass,...

  • Read through the code of the class Player, noting it has two instance variables, name and rank, which are of type String and three further instance variables won, drew and lost which are of type int....

    Read through the code of the class Player, noting it has two instance variables, name and rank, which are of type String and three further instance variables won, drew and lost which are of type int. There is also an attribute of the class, points, which does not have a corresponding instance variable. Also note the constructor and methods of the class and what they do. TournamentAdmin class code: public class RankAdmin {    /**     * Constructor for objects of class...

  • Need help creating a Java program with mandatory requirements! VERY IMPORTANT: The George account class instance...

    Need help creating a Java program with mandatory requirements! VERY IMPORTANT: The George account class instance must be in the main class, and the requirement of printing a list of transactions for only the ids used when the program runs(Detailed in the Additional simulation requirements) is extremely important! Thank you so very much! Instructions: This homework models after a banking situation with ATM machine. You are required to create three classes, Account, Transaction, and the main class (with the main...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT