Results 1 to 5 of 5

Thread: Java array help

  1. #1
    Akubara is offline Member
    Join Date
    Feb 2014
    Posts
    2
    Rep Power
    0

    Default Java array help

    Hi guys, I'm new to programming, and I stumbled upon a problem which didn't have a solution at hand, so I'd appreciate it if you guys can tell me what's wrong.

    So, supposedly, some people are taking the temperature every day at the same time, these values are saved to a double type array. For part a, I need to write a method with this head:

    Java Code:
    public static double biggestTemperatureChange(double[] array)
    This method should tell the biggest change in temperature between two consequent days. As help, it says I can assume that the array has at least 2 elements, and that I should use the abs function from math.

    Here's how it went for me:

    Java Code:
    		public static double biggestTemperatureChange(double[] array) {
    			int numberOfDays = array.length;
    			int day = 0;
    			double temperature[] = new double[numberOfDays];
    			double diff[] = new double[numberOfDays];
    			while (day < numberOfDays) {
    				diff[day] = temperature[day] - temperature[day + 1];
    				java.lang.Math.abs(diff[day]);
    				day += 1;
    			}
    			day = 0;
    			double max = 0;
    			while (day < numberOfDays) {
    				if (diff[day] - diff[day + 1] < max) {
    					max = diff[day];
    				}
    				
    			}
    			return max; 
    		
    		
    		
    	}
    Aside from the fact that this code is ugly, is it working properly?

    For the b part, I have to write a main method which uses the biggestTemperatureChange for the array [-5.0, 1.7, 19.0, 16.9]. This main method should also display the result on the command consol.

    Thanks for the reply in advance!

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,617
    Rep Power
    5

    Default Re: Java array help

    Quote Originally Posted by Akubara View Post
    is it working properly?
    You should be able to answer that question yourself. Imagine writing an app for publishing. Are you going to ask the users that? Or just do some testing with border cases, and hope you covered all of them. Then simply handle bug reports.

    Otherwise, I would have to do what you would have to do to determine whether it works in all situations.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Java array help

    Quote Originally Posted by Akubara View Post
    Aside from the fact that this code is ugly, is it working properly?
    When you put in 10 different data sets, does it produce 10 correct results?

    For the b part, I have to write a main method which uses the biggestTemperatureChange for the array [-5.0, 1.7, 19.0, 16.9]. This main method should also display the result on the command consol.
    Well if you get stuck, ask a specific question.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    Akubara is offline Member
    Join Date
    Feb 2014
    Posts
    2
    Rep Power
    0

    Default Re: Java array help

    Uhh, the problem is that I'm not sure how to start on the b at all. And I can't really run the a part of the programm without a main method, so I'm not quite sure about that either. I know this sounds stupid, but I don't know how to call on a method from another class T.T

  5. #5
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,617
    Rep Power
    5

    Default Re: Java array help

    Well, since you declared your method static, it is quite easy to call it. If it is in a class named Foo, then call it like this:

    Java Code:
    double myTemps = new double[] {1,2,3,4,5,10,2,3 4,10}; // They don't have to be whole numbers.
    double change = Foo.biggestTemperatureChange(myTemps);
    If your program works as expected, the result should be 8.

    Regards,
    Jim
    Last edited by jim829; 02-05-2014 at 07:10 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Bit Array to Byte Array in java
    By Umesh Joshi in forum New To Java
    Replies: 1
    Last Post: 11-30-2012, 08:24 AM
  2. Java 2D Array
    By The Dark Dragon in forum New To Java
    Replies: 3
    Last Post: 11-27-2011, 08:00 AM
  3. Java Array
    By lary in forum New To Java
    Replies: 8
    Last Post: 05-10-2011, 10:44 AM
  4. How to transfer 1D array in JAVA to 3D array in C
    By fishwater00 in forum New To Java
    Replies: 0
    Last Post: 07-31-2009, 06:24 PM
  5. how to convert a Java array to a java stack?
    By pompeez in forum New To Java
    Replies: 2
    Last Post: 08-13-2007, 02:41 PM

Posting Permissions

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