Java Jupyter Notebook
Documentation of the Java Notebook
public class Hello {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Hello.main(null);
import java.util.Scanner;
import javax.swing.JOptionPane;
public class AlsoGPACalc{
void gradeCalc(){
String uInputClass= JOptionPane.showInputDialog("How many classes do you have");
int classNumber=Integer.parseInt(uInputClass);
System.out.println("\nYou have "+classNumber+" classes");
if(classNumber!=0){
System.out.println("Now answer some questions about your grades");
}else{
System.out.print("Why are you running this program");
}
gradeForm(classNumber);
}
void gradeForm(int classNumber){
double total=0.0;
String uGrade="";
for(int i = 0; i < classNumber;i++){
uGrade = JOptionPane.showInputDialog("What is your grade in period "+(i+1));
double x = Double.parseDouble(uGrade);
if(x>=90.0){
total+=4.0;
}else if(x>=80.0){
total+=3.0;
}else if(x>=70.0){
total+=2.0;
}else if(x>=60.0){
total+=1.0;
}else{
total+=0.0;
}
}
System.out.println("Your class score total is "+total);
double finalCalc = total/classNumber;
System.out.println("Your GPA is "+finalCalc);
JOptionPane.showMessageDialog(null, "Your GPA is "+finalCalc);
}
public static void main(String [] args){
AlsoGPACalc start1 = new AlsoGPACalc();
start1.gradeCalc();
}
}
AlsoGPACalc.main(null);
import javax.swing.JOptionPane;
public class Tester {
public static void main(String[] args) {
String uInputClass= JOptionPane.showInputDialog("How many classes do you have");
int classNumber=Integer.parseInt(uInputClass);
System.out.println("\n"+classNumber);
if(classNumber==5){
System.out.println("it works");
}
}
}
Tester.main(null);
import javax.swing.JOptionPane;
import java.util.ArrayList;
public class PrimeOrNot {
public static void main(String[] args) {
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);
}
}
System.out.print("Primes between 2 and "+x+": "+"3 ");
String outputStr = "";
for (int num : primes)
{
outputStr = outputStr + num +" " ;
System.out.print(num+" ");
}
JOptionPane.showMessageDialog(null,"Primes between 2 and "+x+": "+"3 "+outputStr );
}
}
PrimeOrNot.main(null);
import javax.swing.JOptionPane;
import java.util.Random;
public class NumGuesser {
Random rand = new Random();
boolean numCorrect = false;
String uGuess = "";
int numGuesses=0;
void run(){
int randNum = rand.nextInt(1000);
while(numCorrect==false){
uGuess = JOptionPane.showInputDialog("Guess the number between 1-1000");
int uGuessNum = Integer.parseInt(uGuess);
if(uGuessNum>randNum){
JOptionPane.showMessageDialog(null, "Guess too high, try again. Previous guess: "+uGuessNum);
numGuesses+=1;
}else if(uGuessNum<randNum){
JOptionPane.showMessageDialog(null, "Guess too low, try again. Previous guess: "+uGuessNum);
numGuesses+=1;
}else if(uGuessNum==randNum){
JOptionPane.showMessageDialog(null, "Correct! It took you "+numGuesses+" guesses to get it correct");
numCorrect=true;
}
}
}
public static void main(String[] args) {
NumGuesser ng = new NumGuesser();
ng.run();
}
}
NumGuesser.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.*;
public class Calculator implements ActionListener{
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JButton addButton = new JButton("Addition");
JButton subtractButton = new JButton("Subtraction");
JButton multiplyButton = new JButton("Multiplication");
JButton divideButton = new JButton("Division");
JTextField field1String = new JTextField(20);
JTextField field2String = new JTextField(20);
JLabel label = new JLabel();
void setup(){
frame.add(panel);
panel.add(field1String);
panel.add(field2String);
panel.add(addButton);
panel.add(subtractButton);
panel.add(multiplyButton);
panel.add(divideButton);
panel.add(label);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addButton.addActionListener(this);
subtractButton.addActionListener(this);
multiplyButton.addActionListener(this);
divideButton.addActionListener(this);
frame.setSize(new Dimension(300, 200));
}
public static void main(String[] args) {
Calculator calc = new Calculator();
calc.setup();
}
@Override
public void actionPerformed(ActionEvent e) {
double field1 = Double.parseDouble(field1String.getText());
double field2 = Double.parseDouble(field2String.getText());
if(e.getSource()==addButton){
String addF = String.valueOf((field1)+(field2));
label.setText(addF);
}else if(e.getSource()==subtractButton){
String subF = String.valueOf((field1)-(field2));
label.setText(subF);
}else if(e.getSource()==multiplyButton){
String mulF = String.valueOf((field1)*(field2));
label.setText(mulF);
}else if(e.getSource()==divideButton){
String divF = String.valueOf((field1)/(field2));
label.setText(divF);
}
}
}
Calculator.main(null);