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.

Results 1 to 3 of 3

Thread: can you please run this code and tell me the result?

  1. #1
    Junior Member
    Join Date
    Jan 2020
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default can you please run this code and tell me the result?

    Hello everyone.
    Struggling with this java assignment:
    Given an array of numbers, every even number in array is distance traveled, every odd number is time traveled. Need to find total distance traveled. Basically need to multiply [0] and [1], [2] and [3] in array and so on, and get the total.

    import java.util.*;
     
    public class MyClass {
      public static int odometer(int [] travel) {
     
        int N = travel.length;
        int distancePerHour = 0;
        int distanceTotal = 0;
            for (int i = 0; i < N; i += 2) {
    	   distancePerHour = travel[i] * travel[i + 1];
    	   distanceTotal += distancePerHour;
    	   }
    	return distanceTotal;
        }
     
    public static void main(String[] args) {
     
    	int [] testarr  = {20,2,30,6,10,7};
    	System.out.println(odometer(testarr));	
    	}
    }

    So the result im getting is 20*2 + 30*6 + 10*7 = 290
    However when i upload this to test server, it returns 170 ! Seems like it's only multiplies last 2 elements of the array. Need second opinion whether my code is working correctly. Many thanks.

  2. #2
    Junior Member
    Join Date
    Jan 2020
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: can you please run this code and tell me the result?

    Result:

    290

    Process finished with exit code 0

  3. #3
    Member
    Join Date
    Jul 2019
    Posts
    36
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default Re: can you please run this code and tell me the result?

    Think again about this time numbers, what exactly it means :

    speed..check_at
    20....... 0:00 pm start, with 20 mph
    30....... 2:00 pm speed changed at
    10....... 6:00 pm speed changed at
    ... ....... 7:00 pm end at

    total time of travel: 7h !

Similar Threads

  1. code is giving me a different result then expected..
    By stanfordjava in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 22nd, 2014, 07:40 PM
  2. Working code But not intended result?
    By stanfordjava in forum What's Wrong With My Code?
    Replies: 10
    Last Post: August 13th, 2014, 03:26 AM
  3. How do you code to compare result?
    By shin777 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 11th, 2013, 09:09 AM
  4. Replies: 15
    Last Post: December 1st, 2012, 10:13 PM