GUI Jupyter Notebook
Documentation of the Java Notebook
import javax.swing.JOptionPane;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.*;
import java.awt.event.*;
public class RockPaperScissors implements ActionListener{
JFrame frame = new JFrame("Rock Paper Scissors");
JPanel panel = new JPanel();
JButton rock = new JButton("Rock");
JButton paper = new JButton("Paper");
JButton scissor = new JButton("Scissor");
JLabel computer = new JLabel();
JLabel result = new JLabel();
void setup() {
frame.add(panel);
panel.add(rock);
panel.add(paper);
panel.add(scissor);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
rock.addActionListener(this);
paper.addActionListener(this);
scissor.addActionListener(this);
panel.add(computer);
panel.add(result);
frame.setSize(new Dimension(400,300));
}
@Override
public void actionPerformed(ActionEvent e) {
Random rand = new Random();
int numInp = rand.nextInt(3);
int guess = 0;
if(numInp == 0){
computer.setText("Computer picked rock | ");
}else if(numInp == 1){
computer.setText("Computer picked paper | ");
}else if(numInp == 2){
computer.setText("Computer picked scissor | ");
}
if (e.getSource() == rock) {
if(numInp == 0){
result.setText("Tie, go again");
}else if (numInp == 1){
result.setText("You lose");
}else if(numInp ==2){
result.setText("You win");
}
} else if (e.getSource() == paper) {
if(numInp == 0){
result.setText("You win");
}else if (numInp == 1){
result.setText("Tie, go again");
}else if(numInp ==2){
result.setText("You lose");
}
}else if (e.getSource() == scissor) {
if(numInp == 0){
result.setText("You lose");
}else if (numInp == 1){
result.setText("You win");
}else if(numInp ==2){
result.setText("Tie, go again");
}
}
}
public static void main(String[] args) {
RockPaperScissors rps = new RockPaperScissors();
rps.setup();
}
}
RockPaperScissors.main(null);
import javax.swing.JOptionPane;
public class BinaryToDecimal {
public static void main(String[] args) {
String uInputNum = JOptionPane.showInputDialog("Enter a binary number to convert to a decimal");
BinaryToDecimal btd = new BinaryToDecimal();
JOptionPane.showMessageDialog(null, btd.convertBinaryStringToDecimalInt(uInputNum));
}
int convertBinaryStringToDecimalInt(String binStr) {
int total = 0;
int length = binStr.length();
for(int i = length-1; i>=0;i--) {
if(binStr.charAt(i)=='1') {
total+= Math.pow((double)2, (double)(length-1-i));
}
}
return total;
}
}
BinaryToDecimal.main(null);
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
import java.math.BigInteger;
public class Menu implements ActionListener {
JFrame frame = new JFrame("Math Functions");
JPanel panel = new JPanel();
JButton f1 = new JButton("Binary to Decimal Converter");
JButton f2 = new JButton("Prime Number In Range");
JButton f3 = new JButton("Odd or Even Checker");
JButton f4 = new JButton("GCD");
JLabel label = new JLabel();
void setup() {
frame.add(panel);
panel.add(f1);
panel.add(f2);
panel.add(f3);
panel.add(f4);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
f1.addActionListener(this);
f2.addActionListener(this);
f3.addActionListener(this);
f4.addActionListener(this);
panel.add(label);
frame.setSize(new Dimension(600,300));
}
public static void main(String[] args) {
Menu m = new Menu();
m.setup();
}
void convertBinaryStringToDecimalInt() {
String uInputNum = JOptionPane.showInputDialog("Enter a binary number to convert to a decimal");
int total = 0;
int length = uInputNum.length();
for(int i = length-1; i>=0;i--) {
if(uInputNum.charAt(i)=='1') {
total+= Math.pow((double)2, (double)(length-1-i));
}
}
JOptionPane.showMessageDialog(frame, "Decimal: "+ total);
}
void primesFunction(){
ArrayList<Integer> primes = new ArrayList<Integer>();
String userInput = JOptionPane.showInputDialog("What range of numbers do you want to test?(2-number)");
int x = Integer.parseInt(userInput);
boolean isPrime=false;
for(int i = 2; i < x; i++){
for(int j = 2; j <= i/2; j++){
isPrime=true;
if (i % j == 0) {
isPrime = false;
break;
}
}
if(isPrime==true){
primes.add(i);
}
}
String outputStr = "";
for (int num : primes)
{
outputStr = outputStr + num +" " ;
}
JOptionPane.showMessageDialog(frame,"Primes between 2 and "+x+": "+"3 "+outputStr );
}
void oddOrEven(){
String inp = JOptionPane.showInputDialog("Enter a number to check");
int inp1 = Integer.parseInt(inp);
if(inp1%2==0){
JOptionPane.showMessageDialog(null, inp1+" is a even number!");
}else{
JOptionPane.showMessageDialog(null, inp1+" is a odd number!");
}
}
void gcdFunction() {
String num1 = JOptionPane.showInputDialog("First number for gcd");
String num2 = JOptionPane.showInputDialog("Second number for gcd");
int num1int = Integer.parseInt(num1);
int num2int = Integer.parseInt(num2);
if(num1int>num2int){
int temp = num1int;
num1int = num2int;
num2int = temp;
}
BigInteger b1 = BigInteger.valueOf(num1int);
BigInteger b2 = BigInteger.valueOf(num2int);
BigInteger gcd = b1.gcd(b2);
JOptionPane.showMessageDialog(null, gcd.intValue());
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == f1) {
convertBinaryStringToDecimalInt();
} else if (e.getSource() == f2) {
primesFunction();
}else if (e.getSource() == f3) {
oddOrEven();
}else if (e.getSource() == f4) {
gcdFunction();
}
}
}
Menu.main(null);