Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

254
Views
How to search through an array with a random number in Java

For my computer science 3 class, the professor is having us create a Binary tree that consist of random numbers and random operators. I have the random numbers part working, but getting random operators is kinda tricky. I am trying to use a switch-case statement and an array to randomize the operators, but the syntax of this all is quite tricky. Is there a certain why I should go about writing this? Should I even use a switch-case?

import java.util.Random;
public class TestArithmetic {
    public static void main(String[] args) {
            Node n = new Plus(new Divide(randConst(), randConst()),
                    new Divide(randConst(), randConst()));
            Node nDivide = new Divide(new Plus(randConst(), randConst()),
                    new Plus(randConst(), randConst()));
            Node nMulti = new Multi(new Plus(randConst(), randConst()),
                    new Plus(randConst(), randConst()));
            Node nMinus = new Minus(new Plus(randConst(), randConst()),
                    new Plus(randConst(), new Const(4.4)));

            System.out.println(n + " = " + n.eval());
            System.out.println(nDivide + " = " + nDivide.eval());
            System.out.println(nMulti + " = " + nMulti.eval());
            System.out.println(nMinus + " = " + nMinus.eval());
        }
    public static Binop randOp(Node lChild, Node rChild) {
        Node[] opArray = {new Plus(), new Minus(), new Multi(), new Divide()};
        Random randOpNum = new Random();
        int randNumOp1 = randOpNum.nextInt(4);
        
        switch (randNumOp1) {
            case 1:
            case 2:
            case 3:
            case 4:
        }
        return new Binop();
    } 
    
    public static Const randConst() {
        int max = 20;
        int min =1;
        Random num = new Random();
        double randNum = num.nextInt(max-min+1) + min;
        return new Const(randNum);
    }
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can take advantage of the fact that constructors are simply functions that take some parameters that return an instance of the class in which they are defined.

As such your Binop subclasses constructors can be modeled as BiFunction<Node, Node, ? extends Binop>

    // we first initialize an array with reference to all Binop subclasses constructors
    private static final List<BiFunction<Node, Node, ? extends Binop>> opArray = new ArrayList<>(){{
                opArray.add(Plus::new);
                opArray.add(Minus::new);
                opArray.add(Multi::new);
                opArray.add(Divide::new);
            }};

    public static Binop randOp(Node lChild, Node rChild) {
            Random randOpNum = new Random();
            int randNumOp1 = randOpNum.nextInt(opArray.size());
            return opArray.get(randNumOp1).apply(lChild, rChild);
    }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!