1. Member
Join Date
Jun 2014
Posts
11
Rep Power
0

Hello I need help with my final assignment. I did about 85-95 percent of the assignment but I still need help with two things. The first is how to do a back up copy of my Arrays. My professor isn't the best of teachers so I don't really understand what he wants me to do and the second is my graph. For some reason it prints the graph but it is incorrect.

This is the question
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. Make a backup copy of the original arrays.
2. Display the original data set, as shown in the table.
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 cities and their profit listed in descending order of the profits.
7. Make a horizontal graph showing the performance of each supermarket

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 six (6) activities stated in the list of requirements.

(b) Design a test class called TestSupermarket that creates a Supermarket object using the arrays as arguments to its constructor. Call the respective methods to carry out the requirements. Note create the arrays in the test class by declaration and assignment.

2. Member
Join Date
Jun 2014
Posts
11
Rep Power
0

This is what my code looks like Class and Tester

Java Code:
```import java.text.NumberFormat;
class Supermarket
{
double[] profit, list, lenght;
String[] city;

String high;
NumberFormat cf = NumberFormat.getCurrencyInstance();
Supermarket(String[] city, double[] profit)
{
this.city = city;
this.profit = profit;

}

@Override
public String toString()
{
return "City" + '\t' + "Profit" + '\n' + city + '\t' +  profit + 'n';
}
public void display()
{
int a = 0;
System.out.println("City\t\t\tProfit\n");
for(a=0;a<profit.length;a++)
{
System.out.print(city[a] + "    ");
System.out.println(cf.format(profit[a]));
}
System.out.println("Total Profits:\t\t " + cf.format(getSum()));
}
public double getSum()
{
double sum = 0;
for(int i=0;i<profit.length;i++)
{
sum = sum + profit[i];
}
return sum;
}
public double average()
{
return getSum()/profit.length;
}
public String aboveAverage()
{
String s = "The cities with profits above average are:\n";
double avg = average();
for(int i=0;i<profit.length;i++)
{
if(profit[i] >= avg)
{
s = s + city[i] + "\n";
}
}
return s;
}
public String findhighValue()
{
double highestValue = profit[0];
for(int i=1; i < profit.length; i++) {
if(profit[i] > highestValue )
{
highestValue = profit[i];
high = city[i];
}
}
return high;
}
/*public void sort()
{
for (int i=0; i<profit.length-1; i++)
{
int minIndex = i;
for (int j=i+1; j<profit.length; j++)
{
if (profit[minIndex] < profit[j])
{
minIndex = j;
}
}
if (minIndex != i)
{
double temp = profit[i];
profit[i] = profit[minIndex];
profit[minIndex] = temp;
}

System.out.println(city[i] + "\t\t"+ cf.format(profit[i]));
}
}*/
public void selectionSort()
{
for (int startScan = 0; startScan < profit.length - 1; startScan++ )
{
int position = findPosition(startScan);
swap(position, startScan);
}
}
int findPosition(int startScanFrom)
{
int position = startScanFrom;

for (int i = startScanFrom + 1; i < profit.length; i++)
{
if (profit[i] > profit[position])
{
position = i;
}
}

return position;
}

void swap(int i, int j)
{

double value = profit[i];
profit[i] = profit[j];
profit[j] = value;

String str = city[i];
city[i] = city[j];
city[j] = str;
}

String makeGraph()
{

String s = "\t" + "Profits:\n";
for (int i = 0; i < profit.length; i++)
{
s = s + city[i] + "";
int x = (int) Math.floor(profit[i]);

for(int j = 1; j <= i; j++)
{
s = s + "*";

}
s = s + "\n";

}
return s;
}

}```
Java Code:
```import java.text.NumberFormat;
class TestSupermarket
{
public static void main(String arg[])
{
NumberFormat cf = NumberFormat.getCurrencyInstance();

double profit[] = {10200000, 14600000, 17000000, 6000000, 21600000, 9100000, 8000000, 12500000, 2000000, 4500000};
String city[] = {"Miami\t\t", "Sunrise\t\t", "Hollywood\t", "Tallahassee\t", "Jacksonville\t", "Orlando\t\t", "Gainesville\t" ,"Pensacola\t", "Ocala\t\t", "Sebring\t\t"};

Supermarket n = new Supermarket(city, profit);
n.display();
System.out.println("\nAverage of profits: " + cf.format(n.average()));
System.out.println("\nThe city with the highest profit is: " + n.findhighValue());
System.out.println("\n" + n.aboveAverage());
System.out.println("Profits ordered from max to min:");
n.selectionSort();
n.display();
System.out.println("\n");
System.out.println(n.makeGraph());

}
}```

3. ## Re: Help Needed please

Originally Posted by ShinTheWolf
My professor isn't the best of teachers so I don't really understand what he wants me to do
That's part of learning how to program. Breaking a big problem down from "how do I even start this" into smaller, more manageable pieces is a skill that's completely necessary for programming. Many homework assignments will feel like "what is this even asking me" until you build up that skill. Many people here have learned how to program without a teacher, fellow students, text books, tutors, or any of the other perks that come with being a student. So I wouldn't waste a lot of time complaining about your professor, because it sounds like he's doing exactly what he should be.

That being said, you've posted your homework assignment, and you've posted your code, but you haven't really posted a question. What does your code do? Does it compile? Does it throw a runtime exception? Does it exhibit some strange behavior? Something else? What part of the homework are you stuck on?

You say it prints the graph incorrectly. What should the graph look like? What does it look like instead? What happened when you stepped through this with a debugger, or at least added some print statements, to figure out when the program's execution differs from what you expect it to do?

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•