Hello there

Can anyone lend me a hand with this question? i'm seriously not looking for anyone to solve it for me, just a simple explaination will do. I'm having trouble knowing where to start, im just a beginner and this question really caught me by surprise, I don't even know what ceil and truncate are (I looked them up though).

Write programs to compute the following: YOU ARE ONLY ALLOWED TO USE +, -, /, *, %, MATH.floor and Math.Pow. Any other method in Math class should not be used.

- Ceil(x) ---> closest positive number 1.2 ---> 2
- Max (x,y,z) ---> given three numbers, return the largest.
- Min (x,y,z) ---> given three numbers, return the smallest
- Truncate(x)---> truncate the fraction of a real number 1.3 --->1
- Round(x)---> round the numbers 1.4 ---> 1, 1.6 ---> 2
- Absolute(x)
- rightmost(x,n)---> return the n right most digits of x , x=13,n=1 --->3, x=2432342,n=2, --->42
if x has a fraction, ignore the fraction x=1326.4,n=3 --->326
All x,y, z are to be read from the keyboard.

ideas, anyone? so far all I could do is this

import java.util.*;
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner input = new Scanner (System.in);
        System.out.print("Enter x:");
        double x = input.nextDouble();
        System.out.print("Enter x:");
        double y = input.nextDouble();
        System.out.print("Enter x:");
        double z = input.nextDouble();
    }

}

Are you allowed to use if statements? < > signs? If so, min and max should not be a problem - start there :) What exactly have you learned so far in Java, so I will know what you are allowed to use.

Edited 6 Years Ago by apines: n/a

Are you allowed to use if statements? < > signs? If so, min and max should not be a problem - start there :) What exactly have you learned so far in Java, so I will know what you are allowed to use.

Yes, we are. (: as for what we have learned so far. well, we just started learning the loops? I actually think i'd be able to solve the max/min/round/abs easily if it weren't for the "YOU ARE ONLY ALLOWED TO USE +, -, /, *, %, MATH.floor and Math.Pow" so i can't possibly use Math.round or Math.abs..etc! this is where i'm lost. and thank you for your reply!

for now i'll try doing the max and min using if as you suggested, will be back after getting it right. :)

Edited 6 Years Ago by masamune: n/a

Well the point is to learn how to code function and not learn how to use the java library. Although later on you will almost always. Here is some psuedo code that might help:

Function max(a,b,c)
  max(a,b)->x
  return max(c,x)
End Function

Function min(a,b,c)
  min(a,b)->x
  return min(c,x)
End Function

Function Ceil(x)
  return integerPartOf(x+1)
End Function

Function Trunicate(x)
 return integerPartOf(x)
End Function

I'll let you take it from there.

These are nice puzzles to solve. I would start by writing "stubs" for each of the methods you'll have to write - a "stub" is a placeholder method, which returns an answer known to be wrong. This gives you a starting point: you have something that will compile and run. It'll return the wrong answer, but you can fix that next.

so you'd have

public static int max(int a, int b, int c)
{
  return 0;
}

public static int ceil(double x)
{
  return 0;
}

Then whip up a quick little test routine that will run out of your main method. That will simply start this class up and test your methods against some known values. This way, you don't have to keep entering test values over and over again.

This might look something like

public static void main (String [] args)
{
  if (args[0].equals("test"))  // this allows you to run your tests
  {
    runTests();
  }
}

public static void runTests()
{
  System.out.print("max (1,2,3) --- expected 3, got "+max(1,2,3));
  System.out.println("\t\t"+ max(1,2,3)==3?"PASSED!":"FAILED.");
  

  System.out.print("ceil (1.3) --- expected 2, got "+ceil(1.3));
  System.out.println("\t\t"+ ceil(1.3)==2?"PASSED!":"FAILED.");
}

And so forth. This is a sort of home-made junit, and it's not as efficient as actually using junit, but you know how to do it, and it'll introduce you to a very useful idea. By thinking out the conditions for each function you're trying to write, and writing a set of good tests, you're actually working out the problem before you start writing it. And once you start writing the code, this will make it a lot easier to see where you are on each part of the work, and you won't have to break your train of thought when you go to test your work. You can just run the thing, look at the results, and get back to it.

While you're writing tests, you might find that you're not sure what the correct behavior is - this is a good thing. It's better to find out early on that you don't know, maybe, what the expected behavior of round() or ceil() is when it comes to negative numbers. Writing test cases helps you think of these things. Then you look them up, and you're solving the right problem.


[NOTE: I've used the ternary operator in the output of the test code:

System.out.println("\t\t"+ ceil(1.3)==2 ? "PASSED!" : "FAILED.");

It occurs to me that you might not have run across this before. A ternary is a useful way to select from a pair of options on the fly. The expression TEST ? TRUE_CONDITION : FALSE_CONDITION evaluates to one of the two CONDITIONS, depending on whether the TEST is true or false. So 1==0 ? "true" : "false" will evaluate as the String "false".]

Edited 6 Years Ago by jon.kiparsky: n/a

This article has been dead for over six months. Start a new discussion instead.