program cannot compile: ------------------------ import java.util.Random; public
ID: 664884 • Letter: P
Question
program cannot compile:
------------------------
import java.util.Random;
public class Product {
// YOUR CODE: instance variables
String name;
final int ITEM_ID=10;
static int ID;
String productDescription;
Shopper waitingList[];
boolean canAdd;
int shopperCount;
// YOUR CODE: the constructor
public Product(String name, String productDescription) {
this.name = name;
this.productDescription = productDescription;
Random rn = new Random();
this.ID = rn.nextInt(99) + 10;
canAdd=true;
shopperCount=0;
}
// YOUR CODE: getName method
public String getName() {
return name;
}
// YOUR CODE: getID method
public int getID() {
return ID;
}
// YOUR CODE: getProductDescription method
public String getProductDescription() {
return productDescription;
}
// YOUR CODE: setName method
public void setName(String name) {
this.name = name;
}
// YOUR CODE: setProductDescription method
public void setProductDescription(String productDescription) {
this.productDescription = productDescription;
}
void addShopper(Shopper newShopper){
if(shopperCount<4)
{
waitingList[shopperCount]=newShopper;
shopperCount++;
}
else
canAdd=false;
}
boolean canAdd(){
return canAdd;
}
// YOUR CODE: toString method
public String toString(){
return "Product Name: "+getName()+", ID:"+ID+", Description: "+getProductDescription();
}
public static void main(String[] args){
Product firstProduct = new Product("Microwave Oven", "Cooks your food");
Product secondProduct = new Product("Toaster", "Toasts your bread");
Product thirdProduct = new Product("Bookcase", "Holds your books");
System.out.printf("The 1st product's name is %s. ", firstProduct.getName());
System.out.printf("The 2nd product's ID is %d. ", secondProduct.getID());
System.out.print("And the 3rd product... ");
System.out.println(thirdProduct);
System.out.println();
System.out.printf("The 2nd product's name was %s... ", secondProduct.getName());
secondProduct.setName("Blender");
System.out.printf("...but it is now %s. ", secondProduct.getName());
}
}
-------------------------------------
import java.util.Random;
public class Shopper {
// YOUR CODE: instance variables
String name;
int ID=10;
int shopperID;
Product[] product;
boolean canAdd;
int count;
// YOUR CODE: The constructor
Shopper(String name){
this.name=name;
Random rn = new Random();
this.shopperID = rn.nextInt(99) + 10;
canAdd=true;
count=0;
}
// YOUR CODE: getName method
public String getName() {
return name;
}
// YOUR CODE: getID method
public int getID() {
return shopperID;
}
// YOUR CODE: setName method
public void setName(String name) {
this.name = name;
}
Void addProduct(Product newProduct)
{
if(count<10)
{
product[count]=newProduct;
count++;
}
else
canAdd=false;
}
boolean canAdd()
{
return canAdd;
}
// YOUR CODE: toString method
public String toString(){
return "Name : "+getName()+", ID : "+getID();
}
public static void main(String[] args){
Shopper firstShopper = new Shopper("Jane Doe");
Shopper secondShopper = new Shopper("John Doe");
Shopper thirdShopper = new Shopper("Rob Doe");
System.out.printf("The 1st Shopper's name is %s. ", firstShopper.getName());
System.out.printf("The 2nd Shopper's ID is %d. ", secondShopper.getID());
System.out.print("And the 3rd Shopper... ");
System.out.println(thirdShopper);
System.out.println();
System.out.printf("The 2nd Shopper's name was %s... ", secondShopper.getName());
secondShopper.setName("William Shakespeare");
System.out.printf("...but it is now %s. ", secondShopper.getName());
}
}
-----------------------
public class Tester {
public static void main(String[] args){
Product[] products = {
new Product("Microwave Oven", "Cooks your food"),
new Product("Toaster", "Toasts your bread"),
new Product("Bookcase", "Holds your books"),
new Product("Blender", "Blends stuff"),
new Product("Television", "Lets you watch TV"),
new Product("Air Conditioner", "Keeps you cool")
};
Shopper[] shoppers = {
new Shopper("Monica"),
new Shopper("Erica"),
new Shopper("Rita"),
new Shopper("Tina"),
new Shopper("Sandra"),
new Shopper("Mary"),
new Shopper("Jessica")
};
System.out.println("*********************************************");
System.out.println("***Testing Shopper class addProduct method***");
System.out.println("********************************************* ");
testShoppersAddingProducts(shoppers, products);
System.out.println("*********************************************");
System.out.println("***Testing Product class addShopper method***");
System.out.println("********************************************* ");
testProductsWaitingLists(products, shoppers);
}
private static void testShoppersAddingProducts(Shopper[] shoppers, Product[] products){
for (int j = 0; j < 15; j++){
Shopper s = shoppers[randomIntInRange(0, shoppers.length-1)];
if (s.canAdd()){
System.out.printf("%s (ID: #%d) has started shopping... ", s.getName(), s.getID());
int productsToGet = randomIntInRange(0, (products.length-1)/2);
for (int i = 0; i <= productsToGet; i++){
Product p = products[i];
if (s.addProduct(p))
System.out.printf("%s has purchased %s (ID: #%d). ", s.getName(), p.getName(), p.getID());
else
break;
}
System.out.printf("%s is finished shopping. ", s.getName());
}
else
System.out.printf("%s (ID: #%d) has a full cart and cannot shop anymore. ", s.getName(), s.getID());
}
}
private static void testProductsWaitingLists(Product[] products, Shopper[] shoppers){
for (int j = 0; j < 15; j++){
Product p = products[randomIntInRange(0, products.length-1)];
if (p.canAdd()){
System.out.printf("Adding to waiting list of %s (ID: #%d)... ", p.getName(), p.getID());
int shoppersToGet = randomIntInRange(0, (shoppers.length-1)/2);
for (int i = 0; i <= shoppersToGet; i++){
Shopper s = shoppers[i];
if (p.addShopper(s))
System.out.printf("%s added shopper to waiting list: %s (ID: #%d). ", p.getName(), s.getName(), s.getID());
else
break;
}
System.out.printf("%s is finished adding shoppers. ", p.getName());
}
else
System.out.printf("%s (ID: #%d) has a full waiting list and cannot add anymore. ", p.getName(), p.getID());
}
}
private static int randomIntInRange(int low, int high) {
int multiplier = high - (low - 1);
return (int)(Math.random() * multiplier) + low;
}
}
------------------------
when i compile it, it shows
File: C:UsersSteinsDesktopcs110Shopper.java [line: 46]
Error: missing return statement
,how can i fix the program.
thanks
Explanation / Answer
HINT:I had given answer for 1question alone.
1 Question:
public class Shopper
{
String name;
int ID=10;
int shopperID;
Product[] product;
boolean canAdd;
int count;
Shopper(String name){
this.name=name;
Random rn = new Random();
this.shopperID = rn.nextInt(99) + 10;
canAdd=true;
count=0;
}
public String getName() {
return name;
}
public int getID() {
return shopperID;
}
public void setName(String name) {
this.name = name;
}
void addProduct(Product newProduct)
{
if(count<10)
{
product[count]=newProduct;
count++;
}
else
canAdd=false;
}
boolean canAdd()
{
reutn canAdd;
}
public String toString(){
return "Name : "+getName()+", ID : "+getID();
}
public static void main(String[] args){
Shopper firstShopper = new Shopper("Jane Doe");
Shopper secondShopper = new Shopper("John Doe");
Shopper thirdShopper = new Shopper("Rob Doe");
System.out.printf("The 1st Shopper's name is %s. ", firstShopper.getName());
System.out.printf("The 2nd Shopper's ID is %d. ", secondShopper.getID());
System.out.print("And the 3rd Shopper... ");
System.out.println(thirdShopper);
System.out.println();
System.out.printf("The 2nd Shopper's name was %s... ", secondShopper.getName());
secondShopper.setName("William Shakespeare");
System.out.printf("...but it is now %s. ", secondShopper.getName());
}
}
public class Product {
String name;
final int ITEM_ID=10;
static int ID;
String productDescription;
Shopper waitingList[];
boolean canAdd;
int shopperCount;
public Product(String name, String productDescription) {
this.name = name;
this.productDescription = productDescription;
Random rn = new Random();
this.ID = rn.nextInt(99) + 10;
canAdd=true;
shoppercount=0;
}
public String getName() {
return name;
}
getID method
public int getID() {
return ID;
}
public String getProductDescription() {
return productDescription;
}
public void setName(String name) {
this.name = name;
}
public void setProductDescription(String productDescription) {
this.productDescription = productDescription;
}
void addShopper(Shopper newShopper)
{
if(shopperCount<4)
{
waitingList[shopperCount]=newShopper;
shopperCount++;
}
else
canAdd=false;
}
boolean canAdd()
{
return canAdd;
}
public String toString(){
return "Product Name: "+getName()+", ID:"+ID+", Description: "+getProductDescription();
}
public static void main(String[] args){
Product firstProduct = new Product("Microwave Oven", "Cooks your food");
Product secondProduct = new Product("Toaster", "Toasts your bread");
Product thirdProduct = new Product("Bookcase", "Holds your books");
System.out.printf("The 1st product's name is %s. ", firstProduct.getName());
System.out.printf("The 2nd product's ID is %d. ", secondProduct.getID());
System.out.print("And the 3rd product... ");
System.out.println(thirdProduct);
System.out.println();
System.out.printf("The 2nd product's name was %s... ", secondProduct.getName());
secondProduct.setName("Blender");
System.out.printf("...but it is now %s. ", secondProduct.getName());
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.