9 Replies - 3197 Views - Last Post: 02 December 2009 - 07:25 PM Rate Topic: -----

#1 DrDrew  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 30-November 09

Supermarket Profits

Posted 02 December 2009 - 01:17 PM

Hello I am working on this assignment for my Java 1 class but seem to get stuck
I used one of your earlier post to get me started but I seem to be mixing up the variable. Please take a look at my code and see if you may be able to help me here. Thanks in advance.


I TRIED TYPING MY CODE IN THE SPACE PROVEIDE BUT LKEEP GETTONG ERROR MESSAGES
pLEASE SEE ATTACHMENT OFR CODE

The management of a certain chain of supermarkets would like to know on a comparative basis the performance of its supermarkets in each city. The data in the following table shows the amount of profit for all of the supermarkets in each city.

City Profit
Miami $10,200,000.00
Sunrise $14,600,000.00
Hollywood $17,000,000.00
Tallahassee $$6,000,000.00
Jacksonville $21,600,000.00
Orlando $9,100,000.00
Gainesville $8,000,000.00
Pensacola $12,500,000.00
Ocala $2,000,000.00
Sebring $4,500,000.00

The information required must be presented in the following order:
1. Display the original data set, as shown in the table.
2. A horizontal bar graph, showing the performance of each city, to the nearest million dollars.
3. The average profit for the supermarket chain.
4. The city with the highest profit.
5. A list of all the cities with profit at, or above the average.
6. The standard deviation of the profits.
7. The cities and their profit listed in descending order of the profits.

To carryout this exercise:
(a) Design a class called Supermarkets that accepts two arrays - one containing the names of the cities, and the other containing the profits. Write separate methods for each of the seven activities stated in the list of requirements.

(B) Design a test class called TestSupermarket. This class uses the JOptionPane class to read an integer value representing the number of cities, and uses this number to create the arrays. Next, it reads the name of each city with their associated profit amount into the respective arrays. For example, if there were only three cities you would enter:

3
Miami 10200000.00
Sunrise 14600000.00
Hollywood 17000000.00

In this context, each array would be of size three (3), and the values would be read into the arrays.

Create a Supermarkets object using the arrays as arguments to its constructor. Call the respective methods to carryout the requirements.

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: Supermarket Profits

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3116
  • View blog
  • Posts: 19,153
  • Joined: 14-September 07

Re: Supermarket Profits

Posted 02 December 2009 - 01:18 PM

Are you receiving any errors? Does this code not work that way you intended it? When asking for help there are a couple items that are vital in order for someone to properly help you:
  • Post the code you're having problems with
  • Post the exact error you're receiving, if you are receiving one
  • If no error explain what the code is doing versus what you want it to do
  • Post your question in the body of your post, not the description field

Was This Post Helpful? 0
  • +
  • -

#3 DrDrew  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 30-November 09

Re: Supermarket Profits

Posted 02 December 2009 - 01:29 PM

View PostKYA, on 2 Dec, 2009 - 12:18 PM, said:

Are you receiving any errors? Does this code not work that way you intended it? When asking for help there are a couple items that are vital in order for someone to properly help you:
  • Post the code you're having problems with
  • Post the exact error you're receiving, if you are receiving one
  • If no error explain what the code is doing versus what you want it to do
  • Post your question in the body of your post, not the description field

Here is the code I have.
It gives me the following output/error:
Supermarket Chain-Store Survey
Please enter the current store count:
3
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code
at Supermarkets.main(Supermarkets.java:14)
Java Result: 1
BUILD SUCCESSFUL (total time: 3 seconds)


import java.io.*;
import java.util.Scanner;
public class Supermarkets {
static private InputStreamReader in = new InputStreamReader(System.in);
static private BufferedReader br = new BufferedReader(in);
public static void main(String[] Args)throws IOException
{
	SupermarketStoreCount city = new SupermarketStoreCount();
	System.out.println("Supermarket Chain-Store Survey");
	System.out.println("Please enter the current store count:");
	//System.out.println("For example, 7 for July");
	String mon = br.readLine();
	int city = Integer.parseInt(mon);
	date.setCity(city);
	System.out.println("The current month is "+date.getCity());
	System.out.println("Please enter in the profit for a city for the previous  "+city +"  cities: ");
	for(int i=1;i<=city;i++)
	{
		String str = br.readLine();
		int profit = Integer.parseInt(str);
		date.setCity(i);
		System.out.println("Month "+i+" is "+date.getCity()+" and the average profit is "+profit+" dollars.");
		
		}
	}
}
public class SupermarketStoreCount {

	private String city;
	public void setCity(int n){
		if(n==1){
			city="Miami";}
		else if(n==2){
			city="Sunrise";}
		else if(n==3){
			city="Hollywood";}
		else if(n==4){
			city="Tallahassee";}
		else if(n==5){
			city="Jacksonville";}
		else if(n==6){
			city="Orlando";}
		else if(n==7){
			city="Gainesville";}
		else if(n==8){
			city="Pensacola";}
		else if(n==9){
			city="Ocala";}
		else if(n==10){
			city="Sebring";}
		
		}
	public String getCity()
	{
		return city;
	}}
}

Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3116
  • View blog
  • Posts: 19,153
  • Joined: 14-September 07

Re: Supermarket Profits

Posted 02 December 2009 - 01:30 PM

What is date? I don't see that object declared or initialized anywhere.

This post has been edited by KYA: 02 December 2009 - 03:42 PM

Was This Post Helpful? 0
  • +
  • -

#5 DrDrew  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 30-November 09

Re: Supermarket Profits

Posted 02 December 2009 - 01:36 PM

View PostKYA, on 2 Dec, 2009 - 12:30 PM, said:

What is date? I don't see that object declare or initialized anywhere.

That must be debris form the previous post which is where I took the code sample and reworked it for my assignment
I guess I will have to rewrite the code block
Was This Post Helpful? 0
  • +
  • -

#6 DrDrew  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 30-November 09

Re: Supermarket Profits

Posted 02 December 2009 - 04:58 PM

View PostDrDrew, on 2 Dec, 2009 - 12:36 PM, said:

View PostKYA, on 2 Dec, 2009 - 12:30 PM, said:

What is date? I don't see that object declare or initialized anywhere.

That must be debris form the previous post which is where I took the code sample and reworked it for my assignment
I guess I will have to rewrite the code block

OK
Sorry about the delay. Here is my rewritten code

CODE BLOCK ONE:

 public class SupermarketStoreCount {

	private String city;
	public void setCity(int n){
		if(n==1){
			city="Miami";}
		else if(n==2){
			city="Sunrise";}
		else if(n==3){
			city="Hollywood";}
		else if(n==4){
			city="Tallahassee";}
		else if(n==5){
			city="Jacksonville";}
		else if(n==6){
			city="Orlando";}
		else if(n==7){
			city="Gainesville";}
		else if(n==8){
			city="Pensacola";}
		else if(n==9){
			city="Ocala";}
		else if(n==10){
			city="Sebring";}
		
		}
	public String getCity()
	{
		return city;
	}
}

 import java.io.*;
import java.util.Scanner;
public class TestSupermarketStoreCount{
static private InputStreamReader in = new InputStreamReader(System.in);
static private BufferedReader br = new BufferedReader(in);

public static void main(String[] Args)throws IOException
{
	SupermarketStoreCount  store = new SupermarketStoreCount();
	System.out.println("Supermarket Chain-Store Survey");
	System.out.println("Please enter the current store or city code");
	System.out.println("For example, 1 for Miami, 2 Sunrise, 3 Hollywood etc:");
	String cit = br.readLine();
	int city = Integer.parseInt(cit);
	store.setCity(city);
	System.out.println("The requested city / store is "+store.getCity());
	System.out.println("Please enter in the profit for the store in the previous " +city+" cities"+": ");
	for(int i=1;i<=city;i++)
	{
		String str = br.readLine();
		int profit = Integer.parseInt(str);
		store.setCity(i);
		System.out.println("Store "+i+" is "+store.getCity()+" and the Profit is "+profit+" dollars.");
		}
	}
}


The above block of code will generate an output of the following format:

Supermarket Chain-Store Survey
Please enter the current store or city code
For example, 1 for Miami, 2 Sunrise, 3 Hollywood etc:
3
The requested city / store is Hollywood
Please enter in the profit for the store in the previous 3 cities:
13232
Store 1 is Miami and the Profit is 13232 dollars.
66
Store 2 is Sunrise and the Profit is 66 dollars.
23232
Store 3 is Hollywood and the Profit is 23232 dollars.


I want to now use the above output to drive the engine which does the totals, average, min, etc as seen in the following code block

CODE BLOCK TWO:

 import java. util.*;
public class SupermarketStoreProfit
{
   static double max, profitSum;
 
 int min;
 static int length;
   
static String city[]={"Miami","Sunrise","Hollywood","Tallahassee","Jacksonville","Orlando","Gainesville","Pensacola","Ocala","Sebring"};
static double profit[] = {10200000, 14600000, 17000000, 6000000, 21600000, 9100000, 8000000, 12500000, 2000000, 4500000};

public static double average(double profit[])
	{int i;
	double total=0;
	for(i=0;i<10;i++)
		total+= profit[i];
	return total/(profit.length);
	}

	public static int max(double profit[])
	{int i,max=0;
	for(i=1;i<10;i++)
		if(profit[i]>profit[max])
			 max=i;
	return max;
	}

	public static int min(double profit[])
	{int i,max=0;
	for(i=1;i<10;i++)
		if(profit[i]<profit[max])
			 max=i;
	return max;
	}

   	double sumArr()
		{
		double sum = 0;

	for (int i = 0; i < profit.length; i++)
	profitSum = profitSum + profit [i];
	return sum;
	}
}


 import java. util.*;
 public class TestSupermarketStoreProfit
{
	 static String testCity[]   = SupermarketStoreProfit.city.clone();
	 static double testProfit[] = SupermarketStoreProfit.profit.clone();

 public static void main(String[] args)
{
  System.out.println("\nThe values of the array in main are:");
			   System.out.print("\tCity" + "\t"+ "Profit\n");
for (int i = 0; i < testProfit.length; i++)
	System.out.print("\t"+testCity[i] + "\t  " +testProfit[i]+ "\n");
	  System.out.print("\n");
	 
int i=0;
while(testProfit[i]<0);
System.out.println("\nThe average store profit is "+SupermarketStoreProfit.average(testProfit)+" dollars");
System.out.println("The store with the most  profit is  "+testCity[SupermarketStoreProfit.max(testProfit)]);
System.out.println("The store with the least profit is "+testCity[ SupermarketStoreProfit.min(testProfit)]);
}
}

The above code block generated the following output:
The Stores in the Publix Supermarket Florida Chain are:
City Profit
Miami 1.02E7
Sunrise 1.46E7
Hollywood 1.7E7
Tallahassee 6000000.0
Jacksonville 2.16E7
Orlando 9100000.0
Gainesville 8000000.0
Pensacola 1.25E7
Ocala 2000000.0
Sebring 4500000.0


The average store profit is 1.055E7 dollars
The store with the most profit is Jacksonville
The store with the least profit is Ocala

Here is are the issues I am having:
1) I need to be able to use the output of code block one to run the calculation for outputting the results in code block two
2) The results of code block two are rather weird. Is this due to number format issues? If so I can fix it. I just want to ask you to focus on the stuff I can't resolve, as stated above.
Thanks again for your kind and steedy response.
I really appreciate it.

This post has been edited by DrDrew: 02 December 2009 - 05:15 PM

Was This Post Helpful? 0
  • +
  • -

#7 Dreaminjavacode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 02-December 09

Re: Supermarket Profits

Posted 02 December 2009 - 05:15 PM

how would you resolve the number format issues if you dont mind me asking.
Was This Post Helpful? 0
  • +
  • -

#8 DrDrew  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 30-November 09

Re: Supermarket Profits

Posted 02 December 2009 - 05:43 PM

View PostDreaminjavacode, on 2 Dec, 2009 - 04:15 PM, said:

how would you resolve the number format issues if you dont mind me asking.

That is quite easily resolved actually. Here is my updated code block for that:

import java.text.NumberFormat;
import java.text.DecimalFormat;

import java. util.*;
 public class TestSupermarketStoreProfit
{
	 static String testCity[]   = SupermarketStoreProfit.city.clone();
	 static double testProfit[] = SupermarketStoreProfit.profit.clone();

 public static void main(String[] args)
{
  NumberFormat nf  = NumberFormat.getCurrencyInstance();
  DecimalFormat df = (DecimalFormat)nf;
  NumberFormat cf  = NumberFormat.getCurrencyInstance();


	 System.out.println("\nThe Stores in the Publix Supermarket Florida Chain are:");
			   System.out.print("\tCity" + "\t"+ "Profit\n");
for (int i = 0; i < testProfit.length; i++)
	System.out.print("\t"+testCity[i] + "\t  " +cf.format(testProfit[i])+ "\n");
	  System.out.print("\n");
	 
int i=0;
while(testProfit[i]<0);
System.out.println("\nThe average store profit is "+cf.format(SupermarketStoreProfit.average(testProfit))+" dollars");
System.out.println("The store with the most  profit is  "+testCity[SupermarketStoreProfit.max(testProfit)]);
System.out.println("The store with the least profit is "+testCity[ SupermarketStoreProfit.min(testProfit)]);
}
}



Here is my output:
The Stores in the Publix Supermarket Florida Chain are:
City Profit
Miami $10,200,000.00
Sunrise $14,600,000.00
Hollywood $17,000,000.00
Tallahassee $6,000,000.00
Jacksonville $21,600,000.00
Orlando $9,100,000.00
Gainesville $8,000,000.00
Pensacola $12,500,000.00
Ocala $2,000,000.00
Sebring $4,500,000.00


The average store profit is $10,550,000.00 dollars
The store with the most profit is Jacksonville
The store with the least profit is Ocala

Any word on the integration of both code block one and code block two?

This post has been edited by DrDrew: 02 December 2009 - 05:43 PM

Was This Post Helpful? 0
  • +
  • -

#9 DrDrew  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 30-November 09

Re: Supermarket Profits

Posted 02 December 2009 - 06:32 PM

View PostKYA, on 2 Dec, 2009 - 12:30 PM, said:

What is date? I don't see that object declared or initialized anywhere.


Hello there KYA
Sorry about the delay. I just wanted to know if you were still interested in helping me:
I would really appreciate it

View PostDrDrew, on 2 Dec, 2009 - 12:36 PM, said:

View PostKYA, on 2 Dec, 2009 - 12:30 PM, said:

What is date? I don't see that object declare or initialized anywhere.

That must be debris form the previous post which is where I took the code sample and reworked it for my assignment
I guess I will have to rewrite the code block

OK
Sorry about the delay. Here is my rewritten code

CODE BLOCK ONE:

 public class SupermarketStoreCount {

	private String city;
	public void setCity(int n){
		if(n==1){
			city="Miami";}
		else if(n==2){
			city="Sunrise";}
		else if(n==3){
			city="Hollywood";}
		else if(n==4){
			city="Tallahassee";}
		else if(n==5){
			city="Jacksonville";}
		else if(n==6){
			city="Orlando";}
		else if(n==7){
			city="Gainesville";}
		else if(n==8){
			city="Pensacola";}
		else if(n==9){
			city="Ocala";}
		else if(n==10){
			city="Sebring";}
		
		}
	public String getCity()
	{
		return city;
	}
}

 import java.io.*;
import java.util.Scanner;
public class TestSupermarketStoreCount{
static private InputStreamReader in = new InputStreamReader(System.in);
static private BufferedReader br = new BufferedReader(in);

public static void main(String[] Args)throws IOException
{
	SupermarketStoreCount  store = new SupermarketStoreCount();
	System.out.println("Supermarket Chain-Store Survey");
	System.out.println("Please enter the current store or city code");
	System.out.println("For example, 1 for Miami, 2 Sunrise, 3 Hollywood etc:");
	String cit = br.readLine();
	int city = Integer.parseInt(cit);
	store.setCity(city);
	System.out.println("The requested city / store is "+store.getCity());
	System.out.println("Please enter in the profit for the store in the previous " +city+" cities"+": ");
	for(int i=1;i<=city;i++)
	{
		String str = br.readLine();
		int profit = Integer.parseInt(str);
		store.setCity(i);
		System.out.println("Store "+i+" is "+store.getCity()+" and the Profit is "+profit+" dollars.");
		}
	}
}


The above block of code will generate an output of the following format:

Supermarket Chain-Store Survey
Please enter the current store or city code
For example, 1 for Miami, 2 Sunrise, 3 Hollywood etc:
3
The requested city / store is Hollywood
Please enter in the profit for the store in the previous 3 cities:
13232
Store 1 is Miami and the Profit is 13232 dollars.
66
Store 2 is Sunrise and the Profit is 66 dollars.
23232
Store 3 is Hollywood and the Profit is 23232 dollars.


I want to now use the above output to drive the engine which does the totals, average, min, etc as seen in the following code block

CODE BLOCK TWO:

 
import java.text.NumberFormat;
import java.text.DecimalFormat;

import java. util.*;
 public class TestSupermarketStoreProfit
{
	 static String testCity[]   = SupermarketStoreProfit.city.clone();
	 static double testProfit[] = SupermarketStoreProfit.profit.clone();

 public static void main(String[] args)
{
  NumberFormat nf  = NumberFormat.getCurrencyInstance();
  DecimalFormat df = (DecimalFormat)nf;
  NumberFormat cf  = NumberFormat.getCurrencyInstance();


	 System.out.println("\nThe Stores in the Publix Supermarket Florida Chain are:");
			   System.out.print("\tCity" + "\t"+ "Profit\n");
for (int i = 0; i < testProfit.length; i++)
	System.out.print("\t"+testCity[i] + "\t  " +cf.format(testProfit[i])+ "\n");
	  System.out.print("\n");
	 
int i=0;
while(testProfit[i]<0);
System.out.println("\nThe average store profit is "+cf.format(SupermarketStoreProfit.average(testProfit))+" dollars");
System.out.println("The store with the most  profit is  "+testCity[SupermarketStoreProfit.max(testProfit)]);
System.out.println("The store with the least profit is "+testCity[ SupermarketStoreProfit.min(testProfit)]);
}
}


The above code block generated the following output:
The Stores in the Publix Supermarket Florida Chain are:
City Profit
Miami $10,200,000.00
Sunrise $14,600,000.00
Hollywood $17,000,000.00
Tallahassee $6,000,000.00
Jacksonville $21,600,000.00
Orlando $9,100,000.00
Gainesville $8,000,000.00
Pensacola $12,500,000.00
Ocala $2,000,000.00
Sebring $4,500,000.00


The average store profit is $10,550,000.00 dollars

The store with the most profit is Jacksonville
The store with the least profit is Ocala

Here is are the issues I am having:
1) I need to be able to use the output of code block one to run the calculation for outputting the results in code block two
2) The results of code block two are rather weird. Is this due to number format issues? If so I can fix it. I just want to ask you to focus on the stuff I can't resolve, as stated above.
Thanks again for your kind response.
I really appreciate it.

This post has been edited by DrDrew: 02 December 2009 - 06:39 PM

Was This Post Helpful? 0
  • +
  • -

#10 DrDrew  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 30-November 09

Re: Supermarket Profits

Posted 02 December 2009 - 07:25 PM

View PostDreaminjavacode, on 2 Dec, 2009 - 04:15 PM, said:

how would you resolve the number format issues if you dont mind me asking.

Any responses for me yet?
Just checking
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1