0

I am working on a homework assignment that involves getting all values of a and b in range x, and then finding r (a^2 + b^2)/(a*b+1). I need to omit all data r which are not ints and not square roots. I have made a function to omit each, neither work. Here is the code:

/**
 * Compiler to use: javac
 * Name: Mike Fitzgibbon
 * Date: 9/16/2022
 * Class: CS4500
 * 
 * Sources used:
 * I used this one to remind me where the .floor() method was
 * https://www.programiz.com/java-programming/library/math/floor#:~:text=The%20floor()%20method%20rounds,is%20equal%20to%20integer%203.
 */

package mikefitzgibbon.cs4500hw3;

import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int x = getX();
        ArrayList<Double> data[] = generateABR(x);
        data = rIsInt(data);
        data = rIsSquare(data);
        printData(data);
    }
    
    //gets the user input
    private static int getX(){
        Scanner scanner = new Scanner(System.in);
        int x=0;
        boolean xValid = false;
        
        //message to get X
        System.out.println("Please enter an integer 1-100000.");
                
        while(!xValid){
            //get int and make sure it's an int
            String s = scanner.next();
            try{
                x = Integer.parseInt(s);

                //test to make sure x is from 0 to 100000 inclusive
                if(x < 1 || x > 100000){
                    System.out.println("X is not withing the valid range of 1-100000.");
                }
                else xValid = true;
            }
            catch(NumberFormatException e){
                System.out.println("You did not enter an integer.");
            }
        }
        
        return x;
    }
    
    //creates 3 ArrayLists with a, b, and r as the values
    private static ArrayList<Double>[] generateABR(int x){
        //initialize the data structure that will store the variables
        ArrayList<Double> data[] = new ArrayList[3];
        data[0] = new ArrayList<>();//a
        data[1] = new ArrayList<>();//b
        data[2] = new ArrayList<>();//r
        
        //find all pairs of a and b within range x
        for(int a = 1 ; a <= x ; a++){
            for(int b = 1 ; b <= x ; b++){
                data[0].add((double)a);
                data[1].add((double)b);
                //r's formula
                data[2].add((Math.pow(a, 2) + Math.pow(b, 2)) / 
                        (a * b + 1.0));
            }
        }
        
        return data;
    }
    
    //filters out r's that are not ints
    private static ArrayList<Double>[] rIsInt(ArrayList<Double> data[]){
        for(int a = 0 ; a < data[0].size() ; a++){
            //if r of data set is not an int, delete from data structure
            if(data[2].get(a) % 1 != 0){
                data[0].remove(a);
                data[1].remove(a);
                data[2].remove(a);
            }
        }
        
        return data;
    }
    
    //filters out r's that aren't perfect squares
    private static ArrayList<Double>[] rIsSquare(ArrayList<Double> data[]){
        for(int a = 0 ; a < data[0].size() ; a++){
            //if r of data set is not a perfect square, delete from data structure
            if(Math.sqrt(data[2].get(a)) % 1 != 0){
                data[0].remove(a);
                data[1].remove(a);
                data[2].remove(a);
            }
        }
        
        return data;
    }
    
    //prints out the data
    private static void printData(ArrayList<Double> data[]){
        for(int a = 0 ; a < data[0].size() ; a++){
            System.out.println("A: " + Math.round(data[0].get(a)) + " B: " + Math.round(data[1].get(a)) + " R: " + data[2].get(a));
        }
    }
}

and here is an example session:

Please enter an integer 1-100000.
3
A: 1 B: 1 R: 1.0
A: 2 B: 2 R: 1.6
A: 3 B: 3 R: 1.8

The lines with 1.6 and 1.8 should have been removed. So the expected output in this case would be only one line:

A: 1 B: 1 R: 1.0
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • 1
    Please post a [example] instead of your whole code with loops and all. – Federico klez Culloca Sep 17 '22 at 18:08
  • 2
    Removing from lists using indices is fragile since the indices shift when you remove an element. A common solution is to iterate the list *backwards* so only the indices that you have already considered shift. – Ole V.V. Sep 17 '22 at 18:44
  • In my experience mixing arrays and generics doesn’t work well. Also see [Anti-pattern: parallel collections | Jon Skeet's coding blog](https://codeblog.jonskeet.uk/2014/06/03/anti-pattern-parallel-collections/). And consider redesigning. – Ole V.V. Sep 17 '22 at 19:09
  • As an aside comparing floating point numbers such as `double` for equality or inequality as in `data[2].get(a) % 1 !=0 ` often does not give the expected result. See [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken). – Ole V.V. Sep 18 '22 at 03:02
  • For your future Stack Overflow questions it will be a good idea to practice creating a [mre]. See in the linked original question how few lines it takes to reproduce your problem. You should not usually expect Stack Overflow users to debug through nearly 100 lines of code, whereas 3 or 10 lines works well here. – Ole V.V. Sep 18 '22 at 03:16

0 Answers0