public static int GCD(int a, int b) {
if (b == 0) {
return a;
} else {
int a1 = b;
int b1 = a % b;
GCD(a1, b1);
}
return 1;
}
}
Why does this implementation of Euclid's Algo (in Java) always return 1.? How do I get it to return the right answer?
You forgot the return statement. You made the recursive call, but did not use the value.
public static int GCD(int a, int b) {
if (b == 0) {
return a;
} else {
int a1 = b;
int b1 = a % b;
// return here
return GCD(a1, b1);
}
return 1;
}
Although, you can write it succinctly like this:
public static int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
Or in one line:
public static int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
I think you simply missed a return, so you code just runs to the end and returns with 1.
public static int GCD(int a, int b) {
if (b == 0) {
return a;
} else {
int a1 = b;
int b1 = a % b;
return GCD(a1, b1);
}
return 1;
}
}
so I am trying to build this ADT for rational numbers in java but for some reason I keep getting this error that says cannot find symbol when trying to compile. What em I doing wrong? Is this error because of my syntax?
Author: Juan Suarez
// Class : CS1102 ~ java
// Date : 01/30/2018
// Topic : This porblem set focuse on the implemantation of an
// ADT for rational numbers.
public class RationalC implements Rational {
private int num;
private int den;
// ****************** CONSTRUCTOR **********************************
public RationalC (int numerator, int denominator) {
if (this.den == 0){
throw new ArithmeticException("*** WARNING! input non zero denominator");
}
int reduceFraction = gcd(numerator, denominator);
this.num = numerator / reduceFraction;
this.den = denominator / reduceFraction;
if (this.den < 0) {
this.den = this.den * -1;
this.num = this.num * -1;
}
}
//********************* GETTERS ************************************
public int getNumerator() { return this.num; }
public int getDenominator() { return this.den; }
public boolean equal(Rational b) {
boolean
a = this.getNumerator == b.getNumerator;
v = this.getDenominator == b.getDenominator;
return a && v;
}
// ******************* OPERATIONS **********************************
//return this + that
//
public RationalC plus(Rational b) {
int commonDenominator = this.getDenominator() * b.getDenominator();
int num1 = b.getDenominator() * this.getNumerator();
int num2 = b.getNumerator() * this.getDenominator();
int complete = num1 + num2;
return new RationalC (complete, commonDenominator);
}
//returns this - that
//
public RationalC subtract(Rational b) {
int commonDenominator = this.getDenominator() * b.getDenominator();
int num1 = b.getDenominator() * this.getNumerator();
int num2 = b.getNumerator() * this.getDenominator();
int complete = num1 - num2;
return new RationalC (complete, commonDenominator);
}
// return this * that
//
public Rational multiply(Rational b){
int top = this.getNumerator() * b.getNumerator();
int bottom = this.getDenominator() * b.getDenominator();
return new RationalC (top, bottom);
}
//return this / that
//
public Rational divide(Rational b){
int top = this.getNumerator() * b.getDenominator();
int bottom = this.getDenominator() * b.getNumerator();
return new RationalC (top, bottom);
}
//retuns value
//
public boolean equals(Rational b) {
if (num == b.getNumerator() && this.getDenominator() == b.getDenominator() )
return(true);
}
//********************* TOOLS **************************************
//returns the rational number to a string
//
public String toString() {
return "(" + this.num + "," + this.den + ")";
}
//returns -1 , 0, +1 if the value of the rational is <, >, or =
//
public int compareTo(Rational b) {
long leftHand = this.getNumerator() * b.getDenominator();
long rightHand = this.getDenominator() * b.getNumerator();
if (leftHand < rightHand) return -1;
if (leftHand > rightHand) return +1;
return 0;
}
private static int gcd(int m, int n) {
if(m < 0) m = -m;
if(n < 0) n = -n;
return m * (n / gcd(m,n));
}
public Rational reciprical(Rational b){
return new RationalC (this.getDenominator(), this.getNumerator() );
}
//******************* TEST UNIT ************************************
public static void main(String[] args) {
x = new Rational (1,2);
y = new Rational (1,3);
z = x.plus(y);
StdOut.println(z);
}
}
In the below piece of code, you didn't declare local variable v.
public boolean equal(Rational b) {
boolean
a = this.getNumerator == b.getNumerator;
v = this.getDenominator == b.getDenominator;
return a && v;
}
getNumerator and getDenominator are methods.
Call them as this.getNumerator() and this.getDenominator().
Also make sure Rational class is having getNumerator and getDenominator methods.
How to find the median of 7 or 9 numbers? (without any using of Math Methods)
I have already found one way but it's too long but I can't find any other way to solve it. (I swapped the numbers with each other and forced one value to be the median)
This is what i have done. I dont allowed to use API Math Methods and Array Methods.
public class Statistik {
public static double median7(double a, double b, double c, double d, double e, double f, double g) {
double h;
if (a < b) {
h = a;
a = b;
b = h;
}
if (a < c) {
h = a;
a = c;
c = h;
}
if (a < d) {
h = a;
a = d;
d = h;
}
if (a > e) {
h = a;
a = e;
e = h;
if (a < b) {
h = a;
a = b;
b = h;
}
if (a < c) {
h = a;
a = c;
c = h;
}
if (a < d) {
h = a;
a = d;
d = h;
}
}
if (a > f) {
h = a;
a = f;
f = h;
if (a < b) {
h = a;
a = b;
b = h;
}
if (a < c) {
h = a;
a = c;
c = h;
}
if (a < d) {
h = a;
a = d;
d = h;
}
}
if (a > g) {
h = a;
a = g;
g = h;
if (a < b) {
h = a;
a = b;
b = h;
}
if (a < c) {
h = a;
a = c;
c = h;
}
if (a < d) {
h = a;
a = d;
d = h;
}
}
double median7 = a;
return median7;// TODO
}
}
First, you cannot find the median of one single number. So I'm taking it as finding the median of 7 and 9. To do that, just follow this code:
console.log((7 + 9) / 2);
Or, if you want to find the median of a list, then use this code:
function sortNumber(a,b) {
return a - b;
}
let numbersList = []; // plug in numbers
numbersList.sort(sortNumber);
let a = numbersList.length
let b = (a + 1) / 2
console.log(numbersList[b - 1]);
So i have a little problem with reducing a negative fraction
This is my reduce code
private void reduce() {
int g = Helper.gcd(this.num, this.den);
num /= g;
den /= g;
}
For example:
8/64 gives 1/8
But giving -8/64 let's the program crash
This is my gcd code
public static int gcd(int a, int b) {
while (a != b) {
if (a > b) {
a -= b;
} else {
b -= a;
}
}
return a;
}
You need to extract the sign first.
private void reduce() {
boolean neg = (num < 0) != (den < 0);
num = Math.abs(num);
den = Math.abs(den);
// obtain the GCD of the non-negative values.
int g = Helper.gcd(num, den);
num /= g;
den /= g;
if (neg) num *= -1;
}
Your gcd method only works for positive numbers. Negative numbers and zero need to be handled separately.
public static int gcd(int a, int b) {
a = Math.abs(a);
b = Math.abs(b);
if (a == 0) {
if (b == 0)
throw new IllegalArgumentException();
return b;
}
if (b == 0)
return a;
// The rest is just your code, unchanged.
while (a != b) {
if (a > b) {
a -= b;
} else {
b -= a;
}
}
return a;
}
I'm just beginner in programming.
uf is a union-find class with the method union which connects the roots of two node.
This piece of code is responsible for opening a site of a grid and union the site with its neighbor if any of neighbor is opened. And if one of its neighbors is full, then fill all nodes that connected with the site.
This is the actual code:
if(i == 1){
uf.union(len*len, xyTo1D(i,j));
if(existAndOpen(i+1,j)){
uf2.union(xyTo1D(i+1,j), xyTo1D(i,j));
uf.union(xyTo1D(i,j), xyTo1D(i+1,j));
}
if(existAndOpen(i-1,j)){
uf2.union(xyTo1D(i-1,j), xyTo1D(i,j));
uf.union(xyTo1D(i,j), xyTo1D(i-1,j));
}
if(existAndOpen(i,j-1)){
uf2.union(xyTo1D(i,j-1), xyTo1D(i,j));
uf.union(xyTo1D(i,j), xyTo1D(i,j-1));
}
if(!(j == len && i == len)){
if(existAndOpen(i,j+1)){
uf2.union(xyTo1D(i,j+1), xyTo1D(i,j));
uf.union(xyTo1D(i,j), xyTo1D(i,j+1));
}
}
}
else{
if(existAndFull(i+1,j)){
uf2.union(xyTo1D(i+1,j), xyTo1D(i,j));
uf.union(xyTo1D(i,j), xyTo1D(i+1,j));
}
if(existAndFull(i-1,j)){
uf2.union(xyTo1D(i-1,j), xyTo1D(i,j));
uf.union(xyTo1D(i,j), xyTo1D(i-1,j));
}
if(existAndFull(i,j-1)){
uf2.union(xyTo1D(i,j-1), xyTo1D(i,j));
uf.union(xyTo1D(i,j), xyTo1D(i,j-1));
}
if(!(j== len && i == len)){
if(existAndFull(i,j+1)){
uf2.union(xyTo1D(i,j+1), xyTo1D(i,j));
uf.union(xyTo1D(i,j), xyTo1D(i,j+1));
}
}
if(existAndOpen(i+1,j)){
uf.union(xyTo1D(i,j), xyTo1D(i+1,j));
}
if(existAndOpen(i-1,j)){
uf.union(xyTo1D(i,j), xyTo1D(i-1,j));
}
if(existAndOpen(i,j-1)){
uf.union(xyTo1D(i,j), xyTo1D(i,j-1));
}
if(!(j== len && i == len)){
if(existAndOpen(i,j+1)){
uf.union(xyTo1D(i,j), xyTo1D(i,j+1));
}
}
}
}
How can I simplify the code?
Try this
boolean f1(int a, int b) { }
boolean f2(int a, int b) { }
void A(int a, int b) { }
void testAndA(BiPredicate<Integer, Integer> p, int a, int b) {
if (p.test(a, b))
A(a, b);
}
and
if(x == 1){
testAndA(this::f1, x + 1, y);
testAndA(this::f1, x, y + 1);
} else {
testAndA(this::f2, x + 1, y);
testAndA(this::f2, x, y + 1);
}
You can write a loop to loop through all of the different values that could be passed to f1(), such as something like:
for (int deltax = -1; deltax <= 1; deltax++) {
for (int deltay = -1; deltay <= 1; deltay++) {
if (f1(x + deltax, y + deltay)) {
A(x + deltax, y + deltay);
}
}
}
Of course change the start and end values of deltax and deltay depending on which conditions you need to check.
You say there are "more if statements in each block." Reading between the lines, I assume that means you need to make more calls to f1/f2 and A, but with different x and y offsets.
Here is a program that shows a way to refactor the code to avoid coding repetition. Its main features are:
It uses object orientation to abstract the inner if blocks.
It uses an offsets array to represent the x/y offsets for each inner if block.
The doIt() method uses a loop to invoke the inner if blocks.
public class Main {
static interface F {
void f(int i, int j);
}
static class F1Caller implements F {
public void f(int a, int b) {
if (f1(a, b)) {
A(a, b);
}
}
}
static class F2Caller implements F {
public void f(int a, int b) {
if (f2(a, b)) {
A(a, b);
}
}
}
static boolean f1(int a, int b) { System.out.print(" f1. "); return true; }
static boolean f2(int a, int b) { System.out.print(" f2. "); return true; }
static void A(int a, int b) { System.out.println("a: " + a + ", b: " + b); }
static F1Caller f1Caller = new F1Caller();
static F2Caller f2Caller = new F2Caller();
// x and y offsets for each call to f1/f2.
// Add more offset rows, as needed.
static int offsets[][] = {
{1, 0},
{0, -1}
};
static void doIt(int x, int y) {
System.out.println("x: " + x + ", y: " + y);
F f = (x == 1) ? f1Caller : f2Caller;
for (int k = 0; k < offsets.length; k++) {
f.f(x + offsets[k][0], y + offsets[k][1]);
}
}
public static void main(String[] args) {
doIt(0, 0);
doIt(1, 0);
}
}
The output of the above program is:
x: 0, y: 0
f2. a: 1, b: 0
f2. a: 0, b: -1
x: 1, y: 0
f1. a: 2, b: 0
f1. a: 1, b: -1
You can combine the function together.
Using a switch.
public static boolean func(int a, int b, int fun)
{
boolean output = false;
switch(fun)
{
case 1:
//do stuff
output = true;
break;
case 2:
//do stuff
output = true;
break;
default:
//unknown function handling
output = false;
}
return output;
}
Integrate it with a for-loop:
public static void main(String[] args)
{
int i;
//Change this to whatever you want or set it to a argument.
int repeat = 2;
for(i = 1; i <= repeat; i++)
{
func(a, b, i);
}
}