I have to create two classes one class that accepts an object, stores the object in an array. I have another that has a constructor that creates an object.
Here is my first class named "ShoppingList":
import java.util.*;
public class ShoppingList
{
private ShoppingItem [] list;
private int amtItems = 0;
public ShoppingList()
{
list=new ShoppingItem[8];
}
public void add(ShoppingItem item)
{
if(amtItems<8)
{
list[amtItems] =
ShoppingItem(item);
amtItems++;
}
else
System.out.println("Your item's have exceeded the amount of items
that can fit in the
bag.");
}
public void add(String item, int amt, double
cost)
{
if(amtItems<8)
{
list[amtItems] =
new ShoppingItem(item, amt, cost);
amtItems++;
}
else
System.out.println("Your item's have exceeded the amount of items
that can fit in the bag.");
}
public double getTotalCost()
{
double totalCost = 0;
for(int i = 0; i
totalCost+=list[i].getCost();
return totalCost;
}
public String toString()
{
for(int i = 0;i
{
if(i==0)
System.out.println("This shopping list has " + amtItems + "
shopping items: " + list[i].toString());
else
System.out.println(", " + list[i].toString);
}
return"";
}
}
Here is my second class called "ShoppingItem":
public class ShoppingItem
{
private String itemName;
private int amount;
private double pricePU;
public ShoppingItem(String x, int y, double z)
{
this.itemName = x;
this.amount=y;
this.pricePU=z;
}
public double getCost()
{
return
this.amount*this.pricePU;
}
public void setQuantity(int x)
{
this.amount=x;
}
public String toString()
{
return (this.amount + " " +
this.itemName);
}
}
The teacher created a test program to test our algorithms, the constructors in ShoppingItem seem to work. I bolded the area's in ShoppingList that seem to be disagreeing. The second add function was me playing around with different ideas. Her exact line is "g.add( new ShoppingItem("Tissues", 4, 2.30));". So since this part works "ShoppingItem go = new ShoppingItem("Milk", 2, 3.75);" I think my costructor is fine in ShoppingItem. I think my problem is I don't know how to accept an object in my shopping list constructor and save it to my array of objects.
Oh, my .toString function in ShoppingList also has a compiler error if you get a chance to look at it, but I figure I can probably figure that one out.
Here is the teacher's client:
/* Use this client program to test your class
implementations
*
*/
import java.util.*;
public class ShoppingListClient {
public static final int N = 1;
public static final double DISCOUNT = 0.1;
public static void main (String [] args) {
System.out.println("Creating
ShoppingList");
ShoppingList g = new ShoppingList();
System.out.println("Creating new ShoppingItem");
ShoppingItem go = new ShoppingItem("Milk", 2,
3.75);
System.out.println("Changing the quantity to 3");
go.setQuantity( 3 );
System.out.println("The total cost should be 11.25. It
is actually: " + go.getCost() );
System.out.println("Adding ShoppingItems to
ShoppingList");
g.add( go );
g.add( new ShoppingItem("Tissues", 4, 2.30));
g.add (new ShoppingItem
("Toothpaste", 1, 4));
g.add (new ShoppingItem ("Eggs", 12,
0.50));
g.add (new ShoppingItem ("Apples",
5, 2.99));
g.add (new ShoppingItem ("Bananas",
4, 0.99));
System.out.println
(g.toString());
System.out.println("The total cost of shopping list
should be 49.36. It is actually: " + g.getTotalCost() );
Scanner input = new Scanner
(System.in);
System.out.print ("Enter the
customer name: ");
String name = input.next();
for (int i=0; i<N; i++) {
System.out.print
("Enter the shopping item: ");
String
shoppingItemName = input.next();
System.out.print
("Enter the quantity: ");
int quan =
input.nextInt();
System.out.print
("Enter the price per unit: ");
double ppu =
input.nextDouble();
g.add (new
ShoppingItem (shoppingItemName, quan, ppu));
System.out.println
(g.toString());
}
}
}
import java.util.*;
public class ShoppingList
{
private ShoppingItem [] list;
private int amtItems = 0;
public ShoppingList()
{
list=new ShoppingItem[8];
}
public void add(ShoppingItem item)
{
if(amtItems<8)
{
// here you are already passing object of class ShoppingItem and you have an array of
// ShoppingItem type, so you just need to assign ShoppingItem class object item into
// i'th index of array
list[amtItems] = item;
amtItems++;
}
else
System.out.println("Your item's have exceeded the amount of items that can fit in the bag.");
}
public void add(String item, int amt, double cost)
{
if(amtItems<8)
{
list[amtItems] = new ShoppingItem(item, amt, cost);
amtItems++;
}
else
System.out.println("Your item's have exceeded the amount of items that can fit in the bag.");
}
public double getTotalCost()
{
double totalCost = 0;
for(int i = 0; i<amtItems; i++)
totalCost+=list[i].getCost();
return totalCost;
}
public String toString()
{
for(int i = 0;i<amtItems;i++) {
if(i==0)
System.out.println("This shopping list has " + amtItems + " shopping items: " + list[i].toString());
else
System.out.println(", " + list[i].toString()); // in .toString method parenthesis were missing
}
return"";
}
}
I have to create two classes one class that accepts an object, stores the object in...
In this same program I need to create a new method called “int findItem(String[] shoppingList, String item)” that takes an array of strings that is a shopping list and a string for an item name and searches through the “shoppingList” array for find if the “item” exists. If it does exist print a confirmation and return the item index. If the item does not exist in the array print a failure message and return -1. import java.util.Scanner; public class ShoppingList...
Below is the code for the class shoppingList, I need to enhance it to accomplish the challenge level as stated in the description above. public class ShoppingList { private java.util.Scanner scan; private String[] list; private int counter; public ShoppingList() { scan = new java.util.Scanner(System.in); list = new String[10]; counter = 0; } public boolean checkDuplicate(String item) { for(int i = 0; i < counter; i++) { if (list[i].equals(item)) return true; } return false; } public void printList() { System.out.println("Your shopping...
How to create a constructor that uses parameters from different classes? I have to create a constructor for the PrefferedCustomer class that takes parameters(name, address,phone number, customer id, mailing list status, purchase amount) but these parameters are in superclasses Person and Customer. I have to create an object like the example below....... PreferredCustomer preferredcustomer1 = new PreferredCustomer("John Adams", "Los Angeles, CA", "3235331234", 933, true, 400); System.out.println(preferredcustomer1.toString() + "\n"); public class Person { private String name; private String address; private long...
COVERT TO PSEUDOCODE /****************************Vacation.java*****************************/ public abstract class Vacation { /* * private data field */ private double cost; private double budget; private String destination; /** * * @param cost * @param budget * @param destination */ public Vacation(double cost, double budget, String destination) { super(); this.cost = cost; this.budget = budget; this.destination = destination; } //getter and...
Online shopping cart (continued) (Java) Hello, I need help with Java to figure this out. In my Shopping Cart Manager Class (Bottom Code), I get "Resource leak: 'sc' is never closed." I have tried multiple things and cannot figure it out. Thank you. Online shopping cart (continued) (Java) Hello, I need help with Java to figure this out. In my Shopping Cart Manager Class (Bottom Code), I get "Resource leak: 'sc' is never closed." I have tried multiple things and...
Need help with the UML for this code? Thank you. import java.util.Scanner; public class Assignment1Duong1895 { public static void header() { System.out.println("\tWelcome to St. Joseph's College"); } public static void main(String[] args) { Scanner input = new Scanner(System.in); int d; header(); System.out.println("Enter number of items to process"); d = input.nextInt(); ...
java Use the classes given in the previous question and write the output tha t is generated when this program is run. You may write your answer as "Elaine 2 Jerry 1" or "Compiler Error" or "Runtime Error" If there is an error then explain why. public class Main{ public static void main(String args[]){ Object o = new Kramer(); ((George)(o)).method1(); ((Jerry)(o)). method1(); 0.method2(); public class George extends Elaine {! public void method1() { System.out.print("George 1 "); public class Jerry {...
The method m() of class B overrides the m() method of class A, true or false? class A int i; public void maint i) { this.is } } class B extends A{ public void m(Strings) { 1 Select one: True False For the following code, which statement is correct? public class Test { public static void main(String[] args) { Object al = new AC: Object a2 = new Object(); System.out.println(al); System.out.println(a): } } class A intx @Override public String toString()...
Can anyone helps to create a Test.java for the following classes please? Where the Test.java will have a Scanner roster = new Scanner(new FileReader(“roster.txt”); will be needed in this main method to read the roster.txt. public interface List { public int size(); public boolean isEmpty(); public Object get(int i) throws OutOfRangeException; public void set(int i, Object e) throws OutOfRangeException; public void add(int i, Object e) throws OutOfRangeException; public Object remove(int i) throws OutOfRangeException; } public class ArrayList implements List { ...
using Data Structures using Java. Modify it to make the ADT resizable. For this ADT, the MAX_SIZE will not be final, but rather will be increased whenever an new item needs to be added and the array is full (ie. size==MAX_SIZE). Whenever the array needs to grow, add the lesser of: (i) half the current size, or; (ii) 50 additional. Have your ADT report its old and new maximum size whenever the resize operation is called by printing “ADT resized...