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