Multiply objects in an array witrh another object recursively in Java - java

Hello there I am learning Java and after doing some tasks to learn recursion I was giving my self some exercises to learn it a bit more but now I am struggeling with some..
So the main Problem is that I dont know how I can multiply every element in an Array recursively when the elements in that array are object (Maybe there is at the end no difference if objects are in there or not). So the exercise I gave myself was: check if 1 / 3 is in the given Array. If Yes then multiply everything in that array with 2 / 1.
This is Fraction:
private int numerator; // Zaehler
private int denominator; // Nenner
public Fraction ( int num, int denom )
{
if ( denom != 0 )
{
if ( denom < 0 )
{
numerator = -num;
denominator = -denom;
}
else
{
numerator = num;
denominator = denom;
}
reduce();
}
else
{
// error: division by zero
throw new IllegalArgumentException();
}
}
public Fraction()
{
numerator = 0;
denominator = 1;
}
public Fraction( int num )
{
numerator = num;
denominator = 1;
}
So I got it done by doing it with an for loop:
public static Fraction[] mulWithFor(Fraction[] arr)
{
for (int i = 0; i<arr.length; i++)
{
arr[i] = arr[i].multiply(new Fraction(2,1));
}
return arr;
}
But thats not my Main goal I want to do it recursively so that was my approach:
public static Fraction[] mulAus(Fraction[] arr, int i)
{
if (i>= 0 && i<arr.length)
{
rekurMul(arr,i);
//return mulAus(rekurMul(arr,i-1));
}
return arr;
}
public static Fraction rekurMul(Fraction[] arr, int i)
{
if (i>= 0 && i<arr.length)
{
return arr[i].multiply(new Fraction(2,1));
return arr[i].multiply(new Fraction(2, 1)); // Does Not Work!!!
}
throw new IndexOutOfBoundsException();
}
Maybe there is someone who can Help me! Thank you for your attention.
OK Thanks to #Chaï Sarfati and also to the others trying to help me out. I now know how to multiply recursive things in an Array! I used the Methods from #Chaï Sarfati but wrote an alternative method for his "oneThirdIsPresent" which is also a recursive method : So now my working code looks like this
public static Fraction[] mulAus(Fraction[] arr)
{
if(contains(arr,arr.length-1,new Fraction(1,3)))
{
rekurMul(arr,0);
return arr;
}
throw new IllegalArgumentException("1/3 does not exist in the Input-Array");
}
public static void rekurMul(Fraction[] arr, int i)
{
if(i == arr.length)
{
return ;
}
arr[i] = arr[i].multiply(new Fraction(2,1));
rekurMul(arr,i+1);
}
The Method to check if 1 / 3 exists in the given Array.
public static boolean contains(Fraction[] arr, int i, Fraction x)
{
if (i>= 0 && i < arr.length)
{
if (arr[i].equals(x))
{ return true;}
else
{ return contains(arr, i-1,x); }
}
return false;
}
I hope other People can learn from the code.. Maybe there are better solutions but I am just starting Programming so I dont know them for now.
Bye

Assuming you have a multiplyBy(Fraction f) method that works properly in you Fraction class.
Moreover, it will be better (more readable, more time & space complexity saving) to do it iteratively.
For the sake of the example, I would do like this:
First define:
private static boolean oneThirdIsPresent(Fraction[] arr){
for (int i = 0; i < arr.length; i++) {
if(arr[i].numerator == 1 && arr[i].denominator == 3) {
return true;
}
}
return false;
}
private static void recursivelyMultBy2(Fraction[] arr, int index){
if(index == arr.length){
return;
}
arr[index] = arr[index].multiplyBy(new Fraction(2));
recursivelyMultBy2(arr, index+1);
}
In order to solve finally:
public static void multBy2IfOneThirdIsPresent(Fraction[] arr){
if(oneThirdIsPresent(arr)){
recursivelyMultBy2(arr, 0);
}else{
return;
}
}

Here's a quick example of just the recursive multiplication part:
public static void main(String[] args)
{
Fraction[] fractions = new Fraction[] {new Fraction(1,2), new Fraction(2,3), new Fraction(3,1)};
System.out.println("Fractions:");
for(Fraction f: fractions)
{
System.out.println(f);
}
System.out.println("Multiplying array by 2...");
Fraction.mulAus(fractions, new Fraction(2, 1));
for(Fraction f: fractions)
{
System.out.println(f);
}
}
Modified Fraction Class (multiplication code at the bottom):
public class Fraction
{
private int numerator; // Zaehler
private int denominator; // Nenner
public Fraction(int num, int denom)
{
if (denom != 0)
{
if (denom < 0)
{
numerator = -num;
denominator = -denom;
}
else
{
numerator = num;
denominator = denom;
}
reduce();
}
else
{
// error: division by zero
//throw new IllegalArgumentException();
}
}
private void reduce()
{
// ...
}
public Fraction()
{
numerator = 0;
denominator = 1;
}
public Fraction(int num)
{
numerator = num;
denominator = 1;
}
public String toString()
{
return numerator + " / " + denominator;
}
public void MultiplyBy(Fraction F)
{
if (F != null)
{
numerator = numerator * F.numerator;
denominator = denominator * F.denominator;
reduce();
}
}
public static void mulAus(Fraction[] arr, Fraction F)
{
if(arr != null && F != null)
{
rekurMul(arr, 0, F);
}
}
private static void rekurMul(Fraction[] arr, int i, Fraction F)
{
arr[i].MultiplyBy(F);
if (i < (arr.length - 1))
{
rekurMul(arr, ++i, F);
}
}
}
Output:

Related

How to reverse 1000 so that the output will be 1 (not 0001) using recursion or else normal in JAVA

As I have already made the code just I have to add the required condition(1000 == 1 ! 0001). Can anybody help me out.
public class ReverseNum {
static void reverseInteger(int n) {
// Write your code here
if (n <= 0) {
System.out.print("-");
reverseInteger(n * -1);
} else if (n < 10) {
System.out.println(n);
}
else {
System.out.print(n % 10);
reverseInteger(n / 10);
}
}
public static void main (String args[]){
Scanner s = new Scanner(System.in);
int num = s.nextInt();
reverseInteger(num);
}
}
Be careful with negative numbers: they need special consideration (and also will break string reversal and parsing).
A solution that works with any int:
public static int reverse(int value) {
if (value < 0) {
// special handling for negative numbers
return 0 - reverse(-value);
}
int reversed = 0;
while (value > 0) {
reversed = reversed * 10 + (value % 10);
value /= 10;
}
return reversed;
}
Test cases:
assertEquals(0, reverse(0));
assertEquals(321, reverse(123));
assertEquals(98765, reverse(567890000));
assertEquals(-91, reverse(-19));
assertEquals(-2, reverse(-20000));
Try this code :
private static int reverseInteger(int n) {
if (n == 0) {
return n;
}
int symbol = n / Math.abs(n);
n = Math.abs(n);
String str = new StringBuilder(String.valueOf(n)).reverse().toString();
return symbol * Integer.parseInt(str);
}
Test cases:
#Test
public void test_reverseInteger() {
assertEquals(0, reverseInteger(0));
assertEquals(321, reverseInteger(123));
assertEquals(98765, reverseInteger(567890000));
assertEquals(-91, reverseInteger(-19));
assertEquals(-2, reverseInteger(-20000));
}

Java Inheritance Primes Homework

I have a multiphase homework due soon and my I'm on my last leg of it, but I'm very confused by it. The first assignment has me instantiating different Primes that all are dependent on each other and are all effected together no matter which one I refer to. The second part that I'm having trouble with is making it instantiable so that each Prime is effect independently and doing so by making a second class to show that they are independent.
My first class is this:
package project1;
public class Prime {
//Part 2: Primes
//Place your methods in here
public static void main (String[] args) {
Prime p = new Prime();
Prime p2 = new Prime();
p.getPrime();
p2.getPrime();
Prime.getPrime();
p.reset();
p2.getPrime();
p2.sumPrimes(4);
p2.getPrime();
p.getPrime();
}
static int num = 0;
static int count = 0;
public static boolean isPrime(int p) {
if (p <= 1){
return false;
}
for (int i = 2; i < p; i++) {
if (p % i == 0) {
return false;
}
}
return true;
}
public static int getPrime() {
while (Prime.isPrime(num) == false) {
num ++;
}
if (Prime.isPrime(num) == true) {
System.out.println(num);
num ++;
return num;
}
return num;
}
public static void reset() {
num = 0;
}
public static int reset(int n) {
num = n;
while (Prime.isPrime(num) == false) {
num ++;
}
if (Prime.isPrime(num) == true) {
return num;
}
return num;
}
public static int sumPrimes(int n) {
int sum = 0;
while (n > count) {
while (Prime.isPrime(num) == false) {
num ++;
}
if (Prime.isPrime(num) == true) {
sum += num;
count ++;
num ++;
}
}
System.out.println(sum);
return num;
}
}
I know it's not the prettiest code, but I'm a Java beginner. I only need to take this and make it so p and p2 are two separate variables by using instantiation and a new class called PrimeTest that has a main method that shows their independent. Anyone have any tips or examples I can look at?

Find prime factors including their total occurrence (Java)

I was trying to build a Java program for finding the LCM of 'N' numbers. but first of all i am stuck at finding the total prime factors of a number including their occurrences. For example (6=2x3) and (8=2x2x2). but the output i get is '2' for (6) and only two '2's for (8). Where are the other? I am even checking the integer 's' to be prime.
package lcm;
import java.util.ArrayList;
import java.util.Scanner;
public class LCM {
public static boolean isPrime(int numero){
for (int i = 2; i <= Math.sqrt(numero); i++) {
if (numero % i == 0) {
return false;
}
}
return true;
}
public static void factor(int x){
int s;
int copy = x;
ArrayList<Integer> al = new ArrayList<>();
for(s=2;s<copy;s++){
if(copy%s==0){
if (isPrime(s)){
al.add(s);
copy/=s;
//used for repetition
s--;
}
}
}
for( int p : al){
System.out.println(p);
}
}
public static void main(String[] args) {
// TODO code application logic here
int j,k;
int temp=0;
System.out.println("Enter no. of numbers");
Scanner cin = new Scanner(System.in);
int i = cin.nextInt();
int []a = new int[i];
int []b=new int[100];
System.out.println("Enter numbers one by one");
for(j=0;j<a.length;j++){
a[j] = cin.nextInt();
}
for(j=0;j<a.length;j++){
temp=a[j];
factor(temp);
}
}
}
The reason is when s=2 and copy also becomes 2 in a case at that time it skips the loop so only two 2's are shown. Try putting <=copy in that place
I think one of the best way to approach this problem is to use recursion since you continuously have to divide in order to find all the prime factors.
package leastcommonmultiple;
import java.util.ArrayList;
import java.util.Scanner;
public class Runner {
private static LeastCommonMultiple lcm;
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
System.out.println("Enter numbers and separate them with a comma: ");
Scanner cin = new Scanner(System.in);
String[] inputs;
String lineInput;
try {
lineInput = cin.nextLine();
while (lineInput.isEmpty()) {
System.out.println("Enter at least 2 numbers separated by a comma ");
lineInput = cin.nextLine();
}
inputs = lineInput.split(",");
int length = inputs.length;
int[] numbers = new int[length];
int temp = 0;
for (int i = 0; i < length; i++) {
if (inputs[i] != null && !inputs[i].isEmpty()) {
if ((temp = Math.abs(Integer.valueOf(inputs[i].trim()))) == 0) {
throw new IllegalArgumentException("No number should be 0");
}
numbers[i] = temp;
}
}
ArrayList<Integer> list = new ArrayList<>();
lcm = new LeastCommonMultiple();
System.out.println("The Least Common Multiple is: " + lcm.getLeastCommonMultipleOfListOfNumbers(numbers));
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}
package leastcommonmultiple;
import java.util.ArrayList;
public class LeastCommonMultiple {
public LeastCommonMultiple() {
}
/**
* #param numbers array of numbers whose LCM should be found.
* #assure numbers.length > 1
* #return LCM of numbers contained in numbers array
*/
public int getLeastCommonMultipleOfListOfNumbers(int[] numbers) throws IllegalArgumentException {
int leastCommonMultiple;
int length = numbers.length;
if ( length <= 1) {
throw new IllegalArgumentException("Please enter at least 2 numbers separated by a comma.");
} else {
leastCommonMultiple = getLeastCommonMultipleOfTwoNumbers(numbers[0], numbers[length-1]);
length = length-1;
for ( int i=1; i<length; i++ ) {
leastCommonMultiple = getLeastCommonMultipleOfTwoNumbers(leastCommonMultiple, numbers[i]);
}
}
return leastCommonMultiple;
}
private int getLeastCommonMultipleOfTwoNumbers(int number1, int number2) {
int leastCommonMultiple = 0;
int maxOfTheTwoNumbers = Math.max(number1, number2);
int minOfTheTwoNumbers = Math.min(number1, number2);
int quotient = 0;
if (number1 % number2 == 0 || number2 % number1 == 0) {
leastCommonMultiple = maxOfTheTwoNumbers;
} else {
ArrayList<Integer> primeFactors = getPrimeFactors(minOfTheTwoNumbers, new ArrayList<>());
for (int primeFactor : primeFactors) {
if (maxOfTheTwoNumbers % primeFactor == 0) {
maxOfTheTwoNumbers = (maxOfTheTwoNumbers / primeFactor);
}
leastCommonMultiple = minOfTheTwoNumbers * maxOfTheTwoNumbers;
}
}
return leastCommonMultiple;
}
// recursive methods that finds all the prime factors for a given number
/**
* #param numero positive number whose prime factors has to be found
* #param primeFactors an empty ArrayList where the prime factors will be
* stored
* #return the ArrayList containing the found prime factors
*/
private static ArrayList<Integer> getPrimeFactors(int numero, ArrayList<Integer> primeFactors) {
int sqrt = (int) Math.sqrt(numero);
int quotient;
if (isPrime(numero)) {
primeFactors.add(numero);
} else {
if (numero % sqrt == 0) {
quotient = numero / sqrt;
if (isPrime(sqrt)) {
primeFactors.add(sqrt);
} else {
primeFactors = getPrimeFactors(sqrt, primeFactors);
}
} else {
quotient = numero / (sqrt - 1);
if (isPrime(sqrt - 1)) {
primeFactors.add(sqrt - 1);
} else {
primeFactors = getPrimeFactors((sqrt - 1), primeFactors);
}
}
if (!isPrime(quotient)) {
primeFactors = getPrimeFactors(quotient, primeFactors);
} else {
primeFactors.add(quotient);
}
}
return primeFactors;
}
// Make sure a number is prime
public static boolean isPrime(int numero) {
int length = (int) Math.sqrt(numero);
for (int i = 2; i <= length; i++) {
if (numero % i == 0) {
return false;
}
}
return true;
}
}
Here's a solution which is faster by using binary splitting on the Euclidean algorithm:
private static int gcd(int a, int b) {
if (a == 0) return b;
if (b == 0) return a;
return gcd(b, a%b);
}
private static int lcm(int a, int b) {
return a / gcd(a, b) * b;
}
public static int LCM(int[] numbers) {
int len = numbers.length;
if (len == 2) return lcm(numbers[0], numbers[1]);
if (len == 1) return numbers[0];
if (len == 0) return 1;
int[] left = Arrays.copyOfRange(numbers, 0, len/2);
int[] right = Arrays.copyOfRange(numbers, len/2+1, len);
return lcm(LCM(left), LCM(right));
}

Recursive method to add odd numbers

I have the below snippet of code to use a recursive method to add the sum of odd numbers.
I have already coded the iterative method successfully that adds the sum of all odd numbers between n and m which are entered by the user. I'd like to reach that goal but am started slow to make sure I understand what is happening.
I know that it makes more sense to do it iteratively, however I am experimenting with the two types to see which is more efficient. I am stuck on the below as it is not doing what i want it to and i can't understand why.
import java.util.*;
public class SumofOdd
{
public static void main (String [] args)
{
int n = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Please enter an odd number");
n = sc.nextInt();
int x = add(n);
}
public static int add(int x)
{
if (x == 0)
{
return 0;
}
else
{
return (x + add(x-1));
}
}
}
I have changed the above to the below. It compiles however stops after I enter the number.
import java.util.*;
public class SumofOdd
{
public static void main (String [] args)
{
int n = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Please enter an odd number");
n = sc.nextInt();
if (n%2 == 0)
{
System.out.println("The number entered is even");
}
else
{
int x = add(n);
}
}
public static int add(int x)
{
if (x <= 0)
{
return 0;
}
else
{
return (x + add(x-2));
}
}
}
import java.util.*;
public class OddR{
public static void main (String Args [])
{
Scanner s = new Scanner(System.in);
System.out.println("Enter an odd number");
int max = s.nextInt();
if((max% 2) == 0) {
System.out.println(max + " is Even number and therefore is invalid");
}
else{
System.out.println("Enter a greater odd number");
int m = s.nextInt();
if (m <max){
System.out.println("Invalid data");
}
else{
if((m % 2) == 0) {
System.out.println(m + " is Even number and therefore is invalid");
}
else{
int data = (addodd(m)- addodd(max))+max;
System.out.print("sum:"+data);
}
}
}
}
public static int addodd(int m)
{
if(m<=0)
{
return 0;
}
if(m%2 != 0)
{
return (m+addodd(m-1));
}
else
{
return addodd(m-1);
}
}
}
This is the answer recursively of the sum of odd numbers from n to m
public int addOdds(int n) {
if (n <= 0) {
return 0;
}
if (n % 2 == 0) {
return addOdds(n - 1);
}
return x + addOdds(n - 1);
}
Take care, I never tested the code.
class Oddsum {
public int addodd(int n)
{
if(n<=0)
{
return 0;
}
if(n%2 != 0)
{
return (n+addodd(n-1));
}
else
{
return addodd(n-1);
}
}
}
public class Xyz {
public static void main (String[] v)
{
int n = 9;
Oddsum o = new Oddsum();
int data = o.addodd(n);
System.out.print("sum:"+data);
}
}
This is working fine
public static void main (String[] args){
public static int oddSum(int s){
if (s <= 0)
return 0;
else
return s + oddSum(s -2);
}
}

recursive factorial formula

I want to get an output that displays something like 1*2*3*4 but instead I get 4*3*2*1
this is my code:
public static int fact(int n)
{
if(n ==1)
return 1;
else
return n * fact(n-1);
}
public static int factorForm(int n)
{
System.out.print(n);
if (n == 1)
return 1;
else
{
System.out.print("*");
return n + '*' + factorForm(n-1);
}
}
You are calling fact(4)
Then you print
Then you call fact(3)
If you invert that you'll get what you want:
public class fact {
static int f(int n)
{
if (n ==1 )
{
System.out.print(1);
return 1;
}
int ret= (n * f(n-1));
System.out.print("*");
System.out.print(n);
return ret;
}
public static void main(String[] args)
{
int ret=f(4);
System.out.print("=");
System.out.println(ret);
}
}
To reverse the output, n should be printed after making the recursive call:
public static int factorForm(int n)
{
if (n == 1)
{
System.out.print(1);
return 1;
}
else
{
int rest = factorForm(n-1); // prints 1*2*...*n-1
System.out.print("*");
System.out.print(n);
return rest * n;
}
}
The expression n + '*' + factorForm(n-1) performs integer addition, not multiplication or string concatenation. I changed it to perform multiplication. If the intention is to return the string that was printed, the return type and the type of rest should be changed to String, the return value in the base case should be "1", and that expression should be changed to rest + "*" + n.
Return after printing as below, more importantly understand how recursion works:
public static int factorForm(int n)
{
if (n == 1){
System.out.print("1*");
return 1;
}
else
{
int val = n * factorForm(n-1);
System.out.print(n + "*");
return val;
}
}
if you want to get like 1*2*3*4 result. i think you can do this.
this is my code:
public static String fact(int n) {
if (n < 1) {
throw new RuntimeException("n must be int type and up 0");
}
else if (n == 1) {
return "1";
} else {
return n + "*" + fact(n - 1);
}
}
public static String factorForm(String str) {
String [] arr = str.split("\\*");
String [] newArr = new String[arr.length];
String result = "";
if (arr.length > 1) {
for (int i = 0; i < arr.length; i++) {
newArr[arr.length - i - 1] = arr[i];
}
for (int i = 0; i < newArr.length; i++) {
result += newArr[i] + (i != newArr.length - 1 ? "*" : "");
}
return result;
} else {
return str;
}
}
like this. you can get results what you get. may be complicated.

Categories

Resources