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 6 of 6

Thread: Closest to 10

  1. #1
    Junior Member
    Join Date
    Aug 2018
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Closest to 10

    I completed all the requirements expect the last one it's giving me an error: The closeToTen method should display a number on the screen in accordance with the specification. Be sure that the program works correctly with numbers that are equally close to 10.
    Requirements:
    1. The program should display text on the screen.
    2. The main method should not call System.out.println or System.out.print().
    3. The main method should call the closeToTen method.
    4. The closeToTen method should call the abs method.
    5. The closeToTen method should display a number on the screen in accordance with the specification.


        package com.codegym.task.task04.task0409;
     
    /* 
    Closest to 10
     
    */
     
    public class Solution {
        public static void main(String[] args) {
            closeToTen(8, 11);
            closeToTen(7, 14);
        }
     
        public static void closeToTen(int a, int b) {
        	int a1 = abs(a - 10);
        	int b1 = b - 10;
        	if (a1 > b1) {
        		System.out.println(b);
        	} else if (a1 < b1) {
        		System.out.println(a);
        	} else {
        		System.out.println(a + " " + b);
        	}
     
        }
     
        public static int abs(int a) {
            if (a < 0) {
                return -a;
            } else {
                return a;
            }
        }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,318
    Thanks
    53
    Thanked 2,392 Times in 2,346 Posts

    Default Re: Closest to 10

    it's giving me an error:
    Please copy the full text of the error message and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Aug 2018
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Closest to 10

    Screen Shot 2018-08-23 at 8.06.21 PM.jpg
    Quote Originally Posted by Norm View Post
    Please copy the full text of the error message and paste it here.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,318
    Thanks
    53
    Thanked 2,392 Times in 2,346 Posts

    Default Re: Closest to 10

    That does not look like a java programming syntax or run time error.
    What program gave that error?
    Is there a forum where you can ask how to use that program to correct the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Aug 2018
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Closest to 10

    I posted a question in the forum, I didn't want to ask them because this is basically the answer and someone can just copy this and try to figure the rest out. Whatever, I will keep you update and let you know if I get a solution!

    Thank you for trying to help out.

  6. #6
    Junior Member tonya's Avatar
    Join Date
    Feb 2018
    Posts
    14
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Closest to 10

    It ran okay for me, you may want to test out your two numbers closest to 10 case to with something like this

    closeToTen(7, 13);

Similar Threads

  1. Closest Point of Approach (CPA) mathematical formula in ship radar
    By syamilsynz in forum What's Wrong With My Code?
    Replies: 6
    Last Post: April 7th, 2013, 04:27 PM
  2. Search closest distance to an array of objects
    By Dreamer999 in forum Collections and Generics
    Replies: 1
    Last Post: October 10th, 2012, 12:32 PM
  3. closest pair algorithm implementation
    By srose in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 24th, 2011, 01:11 PM