Num Finder
Num Finder
*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.stream.IntStream;
//NumberFinder Class
//with the help of Binary Search TREE computer finds the number you have
determined btw 1 and 100.
//cause of 2 based log100 is equal the 6,64385618977473 the algorithm
generally founds in 7 tries.
//200030306 TESNİM ALTINDAL
public NumFinder()
{
this.setTitle("<< Number Finder >> " + "(Beetween 1-100)" );
this.setSize(300,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
String[] Probabilities ={"MoreBIGGER","MoreSMALLER","TRUE"};
//array for probabilities
comboBox = new JComboBox<>(Probabilities); //creating combo box
comboBox.addActionListener(this);
this.add(comboBox);
//button for send //can be added but nıt necessary
// buttonSend = new JButton("Send");
// buttonSend.addActionListener(this);
// this.add(buttonSend);
//label for display guess
ValueByComputer = new JLabel("Firstly is it 50?");
this.add(ValueByComputer);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==comboBox) //can be added button code (but not
necessary) : e.getSource()==buttonSend ||
{
if(high>=low)
{
int middlePosition= (low+high)/2;
int currentGuess=numbers[middlePosition];
if(comboBox.getSelectedItem().equals("MoreBIGGER"))
{
// middle number<number to find
low=middlePosition+1;
count++;
}
else if (comboBox.getSelectedItem().equals("MoreSMALLER"))
{
//middle number>number to find
high=middlePosition-1;
count++;
}
else {
count++;
//number to find== middle number
ValueByComputer.setText("I FOUND IT ! It is "+
currentGuess + " \n I have tried "+count +" times.");
//main class
OUT: