my teacher posted this exercise and i still dont understand how to do it. i fixed some of the little things that i knew how to fix but the rest of it im not sure of. can anyone help?
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
my teacher posted this exercise and i still dont understand how to do it. i fixed some of the little things that i knew how to fix but the rest of it im not sure of. can anyone help?
It's better if you post all of your code, see how to in my signature.
also, it's kinda hard to help when we don't know what's going wrong.
What are you trying to do?
Wow, your user name pretty much sums it up. Read this before posting another question: How To Ask Questions The Smart Way
This wouldn't hurt either: SSCCE
sorry its my first post thats why all the code wasnt shown. and if the errors and output isnt what you meant sorry
import java.util.Scanner; public class Test3Program { public static void main(String[] args) { // Replace the ??? with the appropriate constructor. [exception] Scanner myIn = new ???; [/exception] String itemDescription; // Temporary variable to hold each description entered String scanResult; // Temporary String variable to hold each value entered double itemValue; // Temporary double variable to hold each value converted from scanResult int numOfDataItems, // Number of data items to enter/entered minLoc, // Location of the minimum element. maxLoc; // Location of the maximum element. do { System.out.print("Please enter the number of data items (minimum of 10 items): "); // Replace the ??? with the appropriate method to parse the String read into an integer. numOfDataItems = Integer.parseInteger(myIn.nextLine()); } while (numOfDataItems < 10); // Enter at least 10 data points Item[] dataItems = new Item[numOfDataItems]; // Create an array of Items // Replace the ??? with the appropriate item to control the loop to enter all of the data points. [exception] for (int count=0; count<dataItems.???; count++) [/exception] { System.out.print("Please enter a description of data item #"+(count+1)+": "); itemDescription = myIn.nextLine(); System.out.print("Please enter the value of data item #"+(count+1)+": "); scanResult = myIn.nextLine(); itemValue = Double.parseDouble(scanResult); // Replace the ??? with the appropriate identifier for the array index [exception] dataItems[???] = new Item(itemDescription, itemValue); [/exception] } double min = dataItems[0].getValue(), max = dataItems[0].getValue(), sum = dataItems[0].getValue(); minLoc = 0; maxLoc = 0; for (int count = 1; count<dataItems.length; count++) { // Replace the ??? with the appropriate identifier for the array index to accumulate the values in the sum. [exception] sum = sum + dataItems[???].getValue(); [/exception] if (dataItems[count].getValue() < min) { min = dataItems[count].getValue(); minLoc = count; } else if (dataItems[count].getValue() > max) { // Replace the ??? with the appropriate array element [exception] max = dataItems[???].getValue(); [/exception] maxLoc = count; } } [console] double average = sum / numOfDataItems; System.out.println("The highest value of the data values was "+max+"."); System.out.println("The highest value item is described as a(n):" + dataItems[maxLoc].getDescription()+"."); // Replace the ??? with the appropriate identifier System.out.println("The lowest value of the data values was "+min+"."); System.out.println("The lowest value item is described as a(n):" + dataItems[minLoc].getDescription()+"."); System.out.println("The average of the item's data values was "+average+"."); // Replace the ??? with the data item at the position equal to the last digit of your student ID number. System.out.println(???); // Include the description and value of data item. [/console] } }
im not sure if u needed to see this one too but it cant hurt
// Class - Item (used to contain a value and description of the value). public class Item { private String description; private double value; public Item() // Default constructor. { this("", 0.0); } public Item(String description, double value) // Detailed constructor. { setDescription(description); setValue(value); } public void setDescription(String description) // Set the object's description attribute. { this.description = description; } public void setValue(double value) // Set the object's value attribute. { this.value = value; } public String getDescription() // Return a String containing the object's description attributes. { return this.description; } public double getValue() // Return a double containing the object's value attributes. { return this.value; } public String toString() // Return a String containing all of the object's attributes. { return "Description: "+getDescription()+"\n"+ "Value: "+getValue()+"\n"; } }
@kevinworkman
its my first post and ive only been using java for about a month and a half, granted that might be enough time to figure this out but my teacher isnt the best, so i have to go off of guessing alot of the time
Are you scanning from a file or from the console?
If from the console
do something like
Scanner console = new Scanner(System.in);
if from a file
Scanner input = new Scanner("FileName.extension");
and, if from a file, your main method needs to throw a FileNotFoundException
like this
public static void main(String[] args) throws FileNotFoundException
Lazy (December 10th, 2010)
Have an array of Items that are the length of number of items entered by Scanner.
Item[] items = new Item[numOfDataItems];
Have a for loop for each item
for (int i =0; i < items.length; i++)
{
System.out.println("Enter Description for item " + (i+1) + ".");
itemDescription = myIn.nextLine();
System.out.print("Please enter the value of data item #"+(count+1)+": ");
scanResult = myIn.nextLine();
itemValue = Double.parseDouble(scanResult);
items[i] = new Item(itemDescription, itemValue);
}
Lazy (December 10th, 2010)
double min = dataItems[0].getValue(),
double max = dataItems[0].getValue(),
double sum = dataItems[0].getValue();
Lazy (December 10th, 2010)
max = dataItems[???].getValue();
replace ??? with
count
That's your variable in the for loop.
Lazy (December 10th, 2010)
That's why I posted the two links. Did you read them? Where's your SSCCE? What errors are you seeing? Are they Exceptions? Is the program acting differently from what you're expecting? What are you expecting? What happens instead? Point out any line numbers from the Exceptions' stack trace in the SSCCE that you post so that we don't have to count.
I'd recommend creating one. Part of debugging and solving problems such as this involves breaking down the problem into smaller and more manageable pieces which can be fixed. Creating an SSCCE helps you in this process (and quite often just in the process of creating one the problem becomes self evident - if not we are here to help). So for the code you posted, does it compile? If not, break it down until it does. If it does compile, do you get exceptions? What are there? Where are they thrown? Again, break the code down until they are not thrown. If you break it down to an extreme minimum and still can't get it to compile or still get errors, post the code for help with a description of the errors..
Last edited by copeg; December 10th, 2010 at 03:29 PM.
import java.util.Scanner; public class Test3Program { public static void main(String[] args) { // Replace the ??? with the appropriate constructor. Scanner myIn = new Scanner(Item.java); String itemDescription; // Temporary variable to hold each description entered String scanResult; // Temporary String variable to hold each value entered double itemValue; // Temporary double variable to hold each value converted from scanResult int numOfDataItems, // Number of data items to enter/entered minLoc, // Location of the minimum element. maxLoc; // Location of the maximum element. do { System.out.print("Please enter the number of data items (minimum of 10 items): "); // Replace the ??? with the appropriate method to parse the String read into an integer. numOfDataItems = Integer.parseInteger(myIn.nextLine()); } while (numOfDataItems < 10); // Enter at least 10 data points Item[] dataItems = new Item[numOfDataItems]; // Create an array of Items // Replace the ??? with the appropriate item to control the loop to enter all of the data points. for (int count=0; count<dataItems.length; count++) { System.out.print("Please enter a description of data item #"+(count+1)+": "); itemDescription = myIn.nextLine(); System.out.print("Please enter the value of data item #"+(count+1)+": "); scanResult = myIn.nextLine(); itemValue = Double.parseDouble(scanResult); // Replace the ??? with the appropriate identifier for the array index dataItems[count+1] = new Item(itemDescription, itemValue); } double min = dataItems[0].getValue(), max = dataItems[0].getValue(), sum = dataItems[0].getValue(); minLoc = 0; maxLoc = 0; for (int count = 1; count<dataItems.length; count++) { // Replace the ??? with the appropriate identifier for the array index to accumulate the values in the sum. sum = sum + dataItems[count].getValue(); if (dataItems[count].getValue() < min) { min = dataItems[count].getValue(); minLoc = count; } else if (dataItems[count].getValue() > max) { // Replace the ??? with the appropriate array element max = dataItems[count].getValue(); maxLoc = count; } } double average = sum / numOfDataItems; System.out.println("The highest value of the data values was "+max+"."); System.out.println("The highest value item is described as a(n):" + dataItems[maxLoc].getDescription()+"."); // Replace the ??? with the appropriate identifier System.out.println("The lowest value of the data values was "+min+"."); System.out.println("The lowest value item is described as a(n):" + dataItems[minLoc].getDescription()+"."); System.out.println("The average of the item's data values was "+average+"."); // Replace the ??? with the data item at the position equal to the last digit of your student ID number. System.out.println("???"); // Include the description and value of data item. } }
i have 2 exceptions in this after compiling. the "(error)" is where the ^ is supposed to be.
Test3Program.java:16: cannot find symbol
symbol : variable java
location: class Item
Scanner myIn = new(error) Scanner(Item.java);
^
Test3Program.java:33: cannot find symbol
symbol : method parseInteger(java.lang.String)
location: class java.lang.Integer
numOfDataItem(error)s = Integer.parseInteger(myIn.nextLine());
^
2 errors
one other thing im not sure of is if this looks right... line 52/53
// Replace the ??? with the appropriate identifier for the array index
dataItems[count+1] = new Item(itemDescription, itemValue);
What is Item.java? Is it a file? Do you mean "Item.java"? Take a look at the Scanner API to see what kinds of arguments its constructors take.
Similarly, consult the API for Integer. Where do you see a parseInteger method?
Java Platform SE 6