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...

  • Create a class called Customer that includes three pieces of information as instance variables - a...

    Create a class called Customer that includes three pieces of information as instance variables - a firstname (type string), a last name (type string) and a credit limit (double) Your class should have a constructor that initializes the three values. a. Provide a customer that initializes these three instance variables. Validate credit limit; it must be greater than 0.0. b. Provide set and get methods for each instance variable. Validate credit limit; it must be greater than 0.0. Write a...

  • 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...

  • Please use Java Question 3: Person and Customer classes: Design a class named Person with properties...

    Please use Java Question 3: Person and Customer classes: Design a class named Person with properties for holding a person's name, address, and telephone number. Next, design a class named Customer, which is derived from the Person class. The Customer class should have a property for a customer number and a Boolean property indicating whether wishes to be on a mailing list. Demonstrate an object of customer class in a simple GUI application. Write the code include appropriate input validation,...

  • 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...

  • 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 Create a class called SavingsAccount. The class should have a static variable named, annualInterestRate, to...

    JAVA Create a class called SavingsAccount. The class should have a static variable named, annualInterestRate, to store the annual interest rate for all account holders. Each object of the class should contain a private instance variable named, savingsBalance, indicating the amount the saver currently has on deposit. Write methods to perform the following: calculateMonthlyInterest – calculates the monthly interest by multiplying the savingsBalance by annualInterestRate divided by 12. This interest should be added to the balance. depositAmount – allows the...

  • 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...

  • Task 1: 1. Write a generic class named MyList, with a type parameter T. The type...

    Task 1: 1. Write a generic class named MyList, with a type parameter T. The type parameter T should be constrained to an upper bound: the Number class. The class should have as a field an ArrayList of type T. Write the class constructor to create the ArrayList. 2. Write a public method named add, which accepts a parameter of type T. When an argument is passed to the method, add it to the ArrayList. 3. Write a public method...

  • 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...

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