CodeGym
Promotion
CodeGym University
Learning
Course
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Question
  • Reviews
  • About us
Start
Start learning
Start learning now
  • All questions
Inga
Level 3
Silale
  • 01.01.2021
  • 372views
  • 4comments

don't know where is the mistakes. please help!!

Question about the task Minimum of four numbers
Java Syntax,  Level 2,  Lesson 8
Under discussion


Write a function that computes the minimum of four numbers.
The function min(a, b, c, d) should use (call) the function min(a, b)

Hint:
You need to write the body of the two existing min functions.

Requirements:
  • The program should display text on the screen.
  • The min methods should not display text on the screen.
  • The main method should call min(a, b) and min(a, b, c, d).
  • The main method should display the result of the min methods. Each time, on a new line.
  • The min(a, b) method must return the minimum of the numbers a and b.
  • The min(a, b, c, d) method must use the min(a, b) method.
  • The min(a, b, c, d) method must return the minimum of the numbers a, b, c, and d.
package com.codegym.task.task02.task0217; /* Minimum of four numbers */ public class Solution { public static int min(int a, int b, int c, int d) { int result; if (a<=b && a<=c && a<=d){ result = a; }else if (b<=a && b<=c && b<=d){ result = b; }else if (c<=a && c<=b && c<=d){ result = c; }else{ result = d; } return result; } public static int min(int a, int b) { int e; if (a<=b){ e=a; }else{ e=b; } return e; } //write your code here } public static void main(String[] args) throws Exception { System.out.println(min(-20, -10)); System.out.println(min(-20, -10, -30, -40)); System.out.println(min(-20, -10, -30, 40)); System.out.println(min(-40, -10, -30, 40)); } }
0
Comments (4)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
remote87
Level 18 , Sofia, Bulgaria
2 January 2021, 07:30
You have a method returning the min from 2 numbers, why do you need such complicated method to do the same with 4 numbers? As some people have said before me, in the method with 4 parameters, call the method with 2 parameters twice, something like
public static int min(int a, int b, int c, int d){
    int firstMin = min(a, b);
    int secondMin = min(c, d);
    return min(firstMin, secondMin);
}
I hope that this is clear enough ( I'm not good in explanations :D )
0
Vic Timmons
Level 7 , Denver, United States
2 January 2021, 05:51
Maybe try calling min(a, b) from within min(a, b, c, d)?
0
Gellert Varga
Level 23 , Szekesfehervar, Hungary
1 January 2021, 19:14
Line 36: this "}" is not needed here
0
Nouser
Level 36 , Germany
1 January 2021, 14:13
The function min(a, b, c, d) should use (call) the function min(a, b) besides that not fullfilled requirement your code looks good
0
Learn
  • Registration
  • Java Course
  • Help with Tasks
  • Pricing
  • Game Projects
  • Java Syntax
Community
  • Users
  • Articles
  • Forum
  • Chat
  • Success Stories
  • Activity
  • Affiliate Program
Company
  • About us
  • Contacts
  • Reviews
  • Press Room
  • CodeGym for EDU
  • FAQ
  • Support
CodeGym CodeGym is an online course for learning Java programming from scratch. This course is a perfect way to master Java for beginners. It contains 1200+ tasks with instant verification and an essential scope of Java fundamentals theory. To help you succeed in education, we’ve implemented a set of motivational features: quizzes, coding projects, content about efficient learning and Java developer’s career.
Follow us
Interface language
Programmers Are Made, Not Born © 2023 CodeGym
MastercardVisa
Programmers Are Made, Not Born © 2023 CodeGym
This website uses cookies to provide you with personalized service. By using this website, you agree to our use of cookies. If you require more details, please read our Terms and Policy.