Chapter 4

Write a class DataSet that computes the largest and smallest values in

Page 1 of 1

9 Replies - 4419 Views - Last Post: 11 April 2010 - 05:05 PM Rate Topic: -----

#1 jmuckey2  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 14-October 08

Chapter 4

Posted 26 October 2008 - 04:16 PM

//Write a class DataSet that computes the largest and smallest values in a sequence of numbers. Supply methods

void addValue(int x)



int getLargest()



int getSmallest()





Keep track of the smallest and largest values that you've seen so far. Then use the Math.min and Math.max methods to update them in the addValue method. What should you use as initial values? Hint: Integer.MIN_VALUE, Integer.MAX_VALUE.


Write a test program DataSetTester that calls addValue four times and prints the expected and actual results.








public class DataSet
{
	public DataSet()
	{
	final MIN_VALUE
	final MAX_VALUE
		largest = Integer.MIN_VALUE;
		smallest = Integer.MAX_VALUE;
	}
	public void addValue(int x)
	{
		currentMax = Math.max(currentMax = largest);
		currentMin = Math.min(currentMin = smallest);
	}
	public int getLargest()
	{
		return largest;
	}
	public int getSmallest()
	{
		return smallest;
	}
	private int largest;
	private int smallest;
}






C:\Documents and Settings\default\My Documents\Downloads\DataSet.java:5: <identifier> expected
final MIN_VALUE
^
1 error

Tool completed with exit code 1

This post has been edited by William_Wilson: 26 October 2008 - 05:04 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Chapter 4

#2 jjsaw5  Icon User is offline

  • I must break you
  • member icon

Reputation: 90
  • View blog
  • Posts: 3,059
  • Joined: 04-January 08

Re: Chapter 4

Posted 26 October 2008 - 05:06 PM

[rules][/rules]
Was This Post Helpful? 0
  • +
  • -

#3 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 204
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: Chapter 4

Posted 26 October 2008 - 05:07 PM

:code:

there is no type of the variable, such as int, double, long on MIN_VALUE. final simply means the value of the variable can only be set once, but it is not the type.
Was This Post Helpful? 0
  • +
  • -

#4 jmuckey2  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 14-October 08

Re: Chapter 4

Posted 26 October 2008 - 06:20 PM

View PostWilliam_Wilson, on 26 Oct, 2008 - 05:07 PM, said:

:code:

there is no type of the variable, such as int, double, long on MIN_VALUE. final simply means the value of the variable can only be set once, but it is not the type.


i have no idea what you are talking about showm me in my code what you are refeering to
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Chapter 4

Posted 26 October 2008 - 08:09 PM

 final int MIN_VALUE = something;  
 final int MAX_VALUE = something;



you cannot make a variable final without initializing it... and you have to give it a type here "int"
Was This Post Helpful? 0
  • +
  • -

#6 jmuckey2  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 14-October 08

Re: Chapter 4

Posted 26 October 2008 - 08:45 PM

View Postpbl, on 26 Oct, 2008 - 08:09 PM, said:

 final int MIN_VALUE = something;  
 final int MAX_VALUE = something;



you cannot make a variable final without initializing it... and you have to give it a type here "int"


explain further please
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Chapter 4

Posted 26 October 2008 - 09:09 PM

What eslse do you need ? The whole code ?


public class DataSet {

	
	int smallestValue;
	int largestValue;
	
	// constructor of a new DataSet instance
	public DataSet() {
		// init the values to Java min & max
		smallestValue = Integer.MAX_VALUE;
		largestValue = Integer.MIN_VALUE;
	}
	
	// set a new value in the Data set
	public void addValue(int x) {
		// check if it is the smallest value I have received
		if(x < smallestValue)
			smallestValue = x;
		// check if it is the largest value I have received
		if(x > largestValue)
			largestValue = x;
	}
	
	// returns the smallest value registered
	int getSmallestValue() {
		return smallestValue;
	}
	// returns the largest value registered
	int getLargestValue() {
		return largestValue;
	}
	
	// to test
	public static void main(String[] arg) {
		DataSet ds = new DataSet();
		System.out.println("Lowest and largest: " + ds.getSmallestValue() + " " + ds.getLargestValue());
		ds.addValue(10);
		ds.addValue(-100);
		ds.addValue(2300);
		ds.addValue(10000);
		System.out.println("Lowest and largest: " + ds.getSmallestValue() + " " + ds.getLargestValue());
	}
}


Now you can try to remove the main method from the main code and write your own tester
Was This Post Helpful? 2
  • +
  • -

#8 Guest_man*


Reputation:

Re: Chapter 4

Posted 11 April 2010 - 04:30 AM

View Postpbl, on 26 October 2008 - 08:09 PM, said:

What eslse do you need ? The whole code ?


public class DataSet {

	
	int smallestValue;
	int largestValue;
	
	// constructor of a new DataSet instance
	public DataSet() {
		// init the values to Java min & max
		smallestValue = Integer.MAX_VALUE;
		largestValue = Integer.MIN_VALUE;
	}
	
	// set a new value in the Data set
	public void addValue(int x) {
		// check if it is the smallest value I have received
		if(x < smallestValue)
			smallestValue = x;
		// check if it is the largest value I have received
		if(x > largestValue)
			largestValue = x;
	}
	
	// returns the smallest value registered
	int getSmallestValue() {
		return smallestValue;
	}
	// returns the largest value registered
	int getLargestValue() {
		return largestValue;
	}
	
	// to test
	public static void main(String[] arg) {
		DataSet ds = new DataSet();
		System.out.println("Lowest and largest: " + ds.getSmallestValue() + " " + ds.getLargestValue());
		ds.addValue(10);
		ds.addValue(-100);
		ds.addValue(2300);
		ds.addValue(10000);
		System.out.println("Lowest and largest: " + ds.getSmallestValue() + " " + ds.getLargestValue());
	}
}


Now you can try to remove the main method from the main code and write your own tester

Was This Post Helpful? 0

#9 Guest_man*


Reputation:

Re: Chapter 4

Posted 11 April 2010 - 04:33 AM

i have the same problem, but we are not allowed to use the if statement, what should the add value method include if no if statement is present?
Was This Post Helpful? 0

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10183
  • View blog
  • Posts: 37,596
  • Joined: 27-December 08

Re: Chapter 4

Posted 11 April 2010 - 05:05 PM

You could use a ternary operator, which will have the same effect. So for example:
//same as: if(x < smallest) smallest = x;
smallest = (x < smallest)?x:smallest;


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1