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
5 replies to this topic
#1
Posted 12 January 2011 - 02:50 PM
|
|
|
#2
Posted 13 January 2011 - 12:32 AM
Does #1 have to be float?
I ask because, floats are irritating and I much prefer using doubles.
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
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
Cook to live; Live to cook. Code is poetry. Chef and code-monkey,
Gabe
#3
Posted 13 January 2011 - 12:56 AM
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
Cook to live; Live to cook. Code is poetry. Chef and code-monkey,
Gabe
#4
Posted 13 January 2011 - 03:17 AM
GabryelFall said:
Does #1 have to be float?
I ask because, floats are irritating and I much prefer using doubles.
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
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
Posted 13 January 2011 - 09:47 AM
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:
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
Cook to live; Live to cook. Code is poetry. Chef and code-monkey,
Gabe
#6
Posted 15 January 2011 - 07:51 AM
I appreciate your help. Problem was solved thanks!
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









