Jump to content

Java method&func

- - - - -

  • Please log in to reply
5 replies to this topic

#1
lina

lina

    Learning Programmer

  • Members
  • PipPipPip
  • 34 posts
Can someone help solve this prog:

1- Write a Java method that takes an array of float values and determines if all the numbers are different from each other( that is they are distinct).

2- write short Java function isMultiple that takes two long values, n and m and returns true if and only if n is multiple of m, that is n = mi for some integer i

Thank you

#2
GabryelFall

GabryelFall

    Learning Programmer

  • Members
  • PipPipPip
  • 74 posts
Does #1 have to be float?

I ask because, floats are irritating and I much prefer using doubles.

public static void arrayEval(){
        double[] d= new double[] {1.111,1.112,1.113};
//        double[] d2= new double[] {1.111,1.111,1.111};

        int unEqual=0;
        int i;
        for(i=0; i<=2; i++){
            if(d[0] != d[i]){
                unEqual= unEqual +1;
            }
        }
        if(unEqual>=1) {
            System.out.println("The members of this array are not equal.");
        }else {
            System.out.println("The members of this array are equal.");
        }
    }

}

EDIT: Yes, I see that I have the if test d[0] against itself at first. It's late and I'm I'm not thinking 100%. :p

Edited by GabryelFall, 13 January 2011 - 01:16 AM.

~~~~>>>><<<<~~~~
Cook to live; Live to cook. Code is poetry. Chef and code-monkey,
Gabe

#3
GabryelFall

GabryelFall

    Learning Programmer

  • Members
  • PipPipPip
  • 74 posts
I'm also assuming you want, on #2, for i to be like you said, an integer. No floating points.

public static boolean isMultiple(){
        Scanner scan=new Scanner(System.in);
        long l1 = scan.nextLong();
        long l2 = scan.nextLong();
        long rem= l1 % l2;
        boolean mult=true;

        if(l1 == 0){
            System.out.println("Obviously it's a multiple.");
            mult = false;
            return mult;
        }else if(l2 == 0) {
            System.out.println("You can't devide by Zero.");
            mult = false;
            return mult;
        }else if(rem != 0){
            System.out.println("Not a multiple.");
            mult = false;
            return mult;
        }else return mult;

    }

Edited by GabryelFall, 13 January 2011 - 01:33 AM.

~~~~>>>><<<<~~~~
Cook to live; Live to cook. Code is poetry. Chef and code-monkey,
Gabe

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java

GabryelFall said:

Does #1 have to be float?

I ask because, floats are irritating and I much prefer using doubles.


public static void arrayEval(){

        double[] d= new double[] {1.111,1.112,1.113};

//        double[] d2= new double[] {1.111,1.111,1.111};


        int unEqual=0;

        int i;

        for(i=0; i<=2; i++){

            if(d[0] != d[i]){

                unEqual= unEqual +1;

            }

        }

        if(unEqual>=1) {

            System.out.println("The members of this array are not equal.");

        }else {

            System.out.println("The members of this array are equal.");

        }

    }


}

EDIT: Yes, I see that I have the if test d[0] against itself at first. It's late and I'm I'm not thinking 100%. :p

You only check the first member of the array, what if the array is 1.1, 1.2, 1.2
Then you would still say every member of the array is unique.
By the way. Working with floats is the same as doubles, you just add a 'f' behind the number :)


public static void arrayEval(){

        float[] fArray= new float[] {1.111f,1.112f,1.112f};

        boolean unique = true;


        for(int i=0 ; i<fArray.length && unique; i++){

          for(int j=i+1 ; j<fArray.length && unique ; j++){

            if(fArray[i] == fArray[j] ){

              unique = false;

            }

          }

        }


        if(unique){

          System.out.println("Every member is unique");

        } else {

          System.out.println("Not Every member is unique");

        }

    }


---------------------------------------------------

GabryelFall said:


public static boolean isMultiple(){

        Scanner scan=new Scanner(System.in);

        long l1 = scan.nextLong();

        long l2 = scan.nextLong();

        long rem= l1 % l2;

        boolean mult=true;


        if(l1 == 0){

            System.out.println("Obviously it's a multiple.");

            mult = false;

            return mult;

        }else if(l2 == 0) {

            System.out.println("You can't devide by Zero.");

            mult = false;

            return mult;

        }else if(rem != 0){

            System.out.println("Not a multiple.");

            mult = false;

            return mult;

        }else return mult;


    }


Okay you check iif l2 is 0, but you do that after doing l1%l2. And that will allready throw an error. You'd have to test for 0 before doing the mod.
(And longs are just integers with a l/L behind it, capitalised L is preferred because l may resemble 1 or i in certain fonts)

   public static boolean isMultiple(long n, long m){

        if(m==0L){

          System.err.println("Can't divide by zero!");

          return false;

        }

 

        return n%m==0;

    }



#5
GabryelFall

GabryelFall

    Learning Programmer

  • Members
  • PipPipPip
  • 74 posts
Well, I was almost there. :D And for 3 o'clock in the morning, I'd say that wasn't bad. But yea, I see what you're sayin. My mistake :)

EDIT: So, I just looked at your code... At first I balked at someone correcting me. :p noob mistake. lol Thanks for taking the time to correct the work of an continual student. :) wim DC, are ARE a programming god! I bow to your skills. :pinguin:

Edited by GabryelFall, 13 January 2011 - 10:42 AM.

~~~~>>>><<<<~~~~
Cook to live; Live to cook. Code is poetry. Chef and code-monkey,
Gabe

#6
lina

lina

    Learning Programmer

  • Members
  • PipPipPip
  • 34 posts
I appreciate your help. Problem was solved thanks!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users