I haven\'t completed the code yet, I was just trying to test the askMultiplyPrin
ID: 3633776 • Letter: I
Question
I haven't completed the code yet, I was just trying to test the askMultiplyPrint() method to see if its working properly. Though, I keep on getting these compile errors:
Hw19.java:26: cannot find symbol
symbol : method indexOf(java.lang.String,Item[])
location: class Item
int indexNum = (items[i].indexOf(name,items));
^
Hw19.java:69: cannot find symbol
symbol : method indexOf(java.lang.String)
location: class Item
return items[i].indexOf(name);
^
Hw19.java:104: cannot find symbol
symbol : method indexOf(java.lang.String)
location: class Item
return items[i].indexOf(name);
It seems like i'm having trouble with indexOf(). I don't think i fully understand how its suppose to be used. Can you see what i'm doing wrong. The code is below, I highlighted where these errors occur. Also how do you call the indexOf() method in the main method?
//////////////////////////////////code//////////////////////////////
import java.util.Scanner;
/////////////////////////////////////////////////////////////////////
class Hw19
{
//------------------------------------------------------------------
public static void main(String [] args)
{
Item [] items = new Item[7];
items[0] = new UnitItem("tissue",1.22);
items[1] = new BulkItem("chicken,wings",2.57);
items[2] = new BulkItem("chicken,whole",1.80);
items[3] = new UnitItem("jello",2.89);
items[4] = new UnitItem("crackers",2.95);
items[5] = new BulkItem("bananas",1.42);
items[6] = new UnitItem("broth",0.97);
Scanner kb = new Scanner(System.in);
System.out.println("Choose an item: ");
String name = kb.nextLine();
for (int i = 0; i < items.length; i++)
{
int indexNum = (items[i].indexOf(name,items));
if (indexNum != -1)
items[i].askMultiplyPrint();
}
}
//-------------------------------------------------------------------
}// end class Hw19
/////////////////////////////////////////////////////////////////////
abstract class Item
{
protected String name;
//------------------------------------------------------------------
abstract double askMultiplyPrint();
//------------------------------------------------------------------
}// end class Item
////////////////////////////////////////////////////////////////////
class UnitItem extends Item
{
private double unitPrice;
//------------------------------------------------------------------
public UnitItem(String name, double unitPrice)
{
super.name = name;
this.unitPrice = unitPrice;
}
//------------------------------------------------------------------
public double askMultiplyPrint()
{
Scanner kb = new Scanner(System.in);
System.out.println("Choose an item: ");
String item = kb.nextLine();
System.out.println("What is the quantity desired? ");
int quantity = kb.nextInt();
double cost = (double)quantity * unitPrice;
System.out.println("The cost is "+cost+"");
return cost;
}
//--------------------------------------------------------------------
public static int indexOf(String name, Item[] items)
{
name.toLowerCase();
for (int i = 0; i < items.length; i++)
{
int indexNum = items[i].indexof(name);
if (indexNum != -1)
return indexNum;
}
return indexNum = -1;
}
}
//--------------------------------------------------------------------
}// end class UnitItem
/////////////////////////////////////////////////////////////////
class BulkItem extends Item
{
private double bulkPrice;
//-------------------------------------------------------------------
public BulkItem(String name, double bulkPrice)
{
super.name = name;
this.bulkPrice = bulkPrice;
}
//-------------------------------------------------------------------
public double askMultiplyPrint()
{
Scanner kb = new Scanner(System.in);
System.out.println("Choose an item: ");
String item = kb.nextLine();
item.toLowerCase();
System.out.println("What are the pounds desired?: ");
double pounds = kb.nextInt();
double cost = pounds * bulkPrice;
}
//--------------------------------------------------------------------
public static int indexOf(String name, Item[] items)
{
name.toLowerCase();
for (int i = 0; i < items.length; i++)
{
int indexNum = items[i].indexof(name);
if (indexNum != -1)
return indexNum;
}
return indexNum = -1;
}
}
//--------------------------------------------------------------------
}
Explanation / Answer
Hello! Ok I see many many things that are different from my code so its hard to start. Well first off, we are supposed to call static method indexOf() which is STATIC meaning its a method that can be called by putting "classname.staticmethod();" (Omit the "classname" part if you are called the static method within the same class).
So seeing that you have two static methods in each child class, you will need to do UnitItem.indexOf(peram,peram)... but I do not believe that is what he wanted. I stuffed most of my code into the driver, including the indexOf method, so that I would not have to repeat any code in the inherited classes.
Here is my code, post/pm if you have any futher questions. ~Oh and I never got any earlier message besides the comment posted at 5pm. Not sure what happened~
// Written by Cal Poly Pomona Student
//
// Solves CS 141, Fall 2011, Homework 19.
//
package hw19;
import java.io.*;
import java.util.Scanner;
/////////////////////////////////////////////////////////////////////////
class Hw19
{
//-----------------------------------------------------------------------
public static void main(String[] args)
{
Scanner kb=new Scanner(System.in);
Item[] items = new Item[7];
items[0] = new UnitItem("tissue", 1.22);
items[1] = new BulkItem("chicken,wings", 2.57);
items[2] = new BulkItem("chicken,whole", 1.80);
items[3] = new UnitItem("jello", 2.89);
items[4] = new UnitItem("crackers", 2.95);
items[5] = new BulkItem("bananas", 1.42);
items[6] = new UnitItem("broth", 0.97);
double total=0;
System.out.print("Item? ");
String buy=kb.nextLine();
while (!buy.equals("quit"))
{
int location=indexOf(buy,items);
if (location==-1)
System.out.println("Sorry, but we don't carry that item here. ");
if (!(location==-1))
{
double cost=items[location].askMultiplyPrint();
total+=cost;
System.out.printf("Cost is %2.2f ",cost);
}
System.out.print("Item? ");
buy=kb.nextLine();
}
System.out.printf(" Your total cost is %2.2f ",total);
System.out.println("Thank you for shopping with Java!");
}
//-----------------------------------------------------------------------
public static int indexOf(String name, Item[] items)
{
for (int i=0; i<items.length; i++)
{
if (name.equals(items[i].name))
return i;
}
return -1;
}
//-----------------------------------------------------------------------
}
/////////////////////////////////////////////////////////////////////////
abstract class Item
{
protected String name;
//-----------------------------------------------------------------------
public Item(String name)
{
this.name=name;
}
//-----------------------------------------------------------------------
abstract double askMultiplyPrint();
//-----------------------------------------------------------------------
}
/////////////////////////////////////////////////////////////////////////
class UnitItem extends Item
{
double unitPrice;
Scanner kb= new Scanner(System.in);
//-----------------------------------------------------------------------
public UnitItem(String name, double unitPrice)
{
super(name);
this.unitPrice=unitPrice;
}
//-----------------------------------------------------------------------
double askMultiplyPrint()
{
System.out.print("Quantity desired? ");
return kb.nextInt()*unitPrice;
}
//-----------------------------------------------------------------------
}
/////////////////////////////////////////////////////////////////////////
class BulkItem extends Item
{
double bulkPrice;
Scanner kb= new Scanner(System.in);
//-----------------------------------------------------------------------
public BulkItem(String name, double bulkPrice)
{
super(name);
this.bulkPrice=bulkPrice;
}
//-----------------------------------------------------------------------
double askMultiplyPrint()
{
System.out.print("Pounds desired? ");
return kb.nextDouble()*bulkPrice;
}
//-----------------------------------------------------------------------
}
/////////////////////////////////////////////////////////////////////////