Hello Tutor,

What I want to do is make a GUI with my program for gcd. I will upload all the classes for the program but my main question is to have you create a GUI. Look at these lines of code with import javax… to make all the pannels and commands. This is from an online forum from stackoverflow.

/***********************************'' import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.GridLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JPanel; import javax.swing.JSeparator; import javax.swing.JTable; import javax.swing.SwingUtilities; public class GUI extends JFrame { private JButton loadFileBtn = new JButton(“Load File”); private JButton generateReportBtn = new JButton(“Generate Report”); private JButton exitBtn = new JButton(“Exit”); private JLabel fileNameLbl = new JLabel(“File Name Here”); private JMenuBar menuBar = new JMenuBar(); private JMenu fileMI = new JMenu(“File”); private JMenuItem openFileMenu = new JMenuItem(“Open File”); private JSeparator separator = new JSeparator(); private JMenuItem exitMenu = new JMenuItem(“Exit”); private JMenu reportMI = new JMenu(“Report”); private JMenuItem generateReportMenu = new JMenuItem(“Generate Report”); private JMenu helpMI = new JMenu(“Help”); private JMenuItem aboutMenu = new JMenuItem(“About”); private JTable table = new JTable(5, 2); private JPanel mainPanel = new JPanel(new BorderLayout(10, 10)); private JPanel panel1 = new JPanel(new BorderLayout()); private JPanel panel2 = new JPanel(new GridLayout(1, 2)); private JPanel panel3 = new JPanel(new GridLayout(1, 2)); public GUI() { super(“Sample GUI”); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(new Dimension(300, 300)); setResizable(false); setLayout(new BorderLayout(10, 10)); fileMI.add(openFileMenu); fileMI.add(separator); fileMI.add(exitMenu); reportMI.add(generateReportMenu); helpMI.add(aboutMenu); menuBar.add(fileMI); menuBar.add(reportMI); menuBar.add(helpMI); setJMenuBar(menuBar); panel1.add(table, BorderLayout.CENTER); panel2.add(fileNameLbl); panel2.add(loadFileBtn); panel3.add(generateReportBtn); panel3.add(exitBtn); mainPanel.add(panel2, BorderLayout.NORTH); mainPanel.add(panel1, BorderLayout.CENTER); mainPanel.add(panel3, BorderLayout.SOUTH); add(mainPanel); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { GUI app = new GUI(); app.setVisible(true); } }); } }

/**************************************************************************************

The website for this code is from http://stackoverflow.com/questions/15363706/how-to-program-this-gui-in-java-swing. My main question is not how to run the program I have. It is to create a GUI with all the options 0-9 in the pannel. Again I have posted this question three times already and I do not want you to simply show me how to run the program. I want you to import new packages and make a GUI with the options for 0-9. The program is here as follows I will break it up into multiple sections.

/***********************************************************

import java.util.*;

class PJ1_Test
{
static private Scanner scanner;

private static SimpleFraction readFraction() {
System.out.print( “nTry to read a fraction x/y, please enter x y : ” );
int numerator = scanner.nextInt();
int denominator = scanner.nextInt();
SimpleFraction f = new SimpleFraction(numerator, denominator);
System.out.println( “ttRead OK:”+f);
return f;
}

private static void printOperations() {
System.out.println(“==============================================”);
System.out.println(“nOperations:”);
System.out.println(” 0) exit t1) add tt2) subtract t3) multiply t4) divide”);
System.out.println(” 5) compareTo t6) equals t7) recipocal t8) toDouble t9) setSimpleFraction (x/y) “);
System.out.print( “nEnter an operation number: “);
}

public static void main( String args[] )
{
scanner = new Scanner( System.in ); // scanner for input
boolean continueLoop = true; // determines if more input is needed
SimpleFraction n1=null;
SimpleFraction n2=null;
int op,x,y;
  
do
{   
try // read two numbers and calculate quotient   
{
printOperations();
op= scanner.nextInt();

if (op == 0) {
break;
} else if ((op >0) && (op n1 = readFraction();
n2 = readFraction();
} else if ((op > 6) && (op
n1 = readFraction();
} else if (op == 9) {
n1 = new SimpleFraction();
} else {
System.out.print( “nInvalid input… try againn” );
continue;
}

System.out.println(“nTests:n”);
switch (op) {
case 1:
System.out.println(“t” + n1 + ” + ” + n1 + ” = ” + n1.add(n1));
System.out.println(“t” + n2 + ” + ” + n2 + ” = ” + n2.add(n2));
System.out.println(“t” + n1 + ” + ” + n2 + ” = ” + n1.add(n2));
System.out.println(“t” + n2 + ” + ” + n1 + ” = ” + n2.add(n1));
break;
case 2:
System.out.println(“t” + n1 + ” – ” + n1 + ” = ” + n1.subtract(n1));
System.out.println(“t” + n2 + ” – ” + n2 + ” = ” + n2.subtract(n2));
System.out.println(“t” + n1 + ” – ” + n2 + ” = ” + n1.subtract(n2));
System.out.println(“t” + n2 + ” – ” + n1 + ” = ” + n2.subtract(n1));
break;
case 3:
System.out.println(“t” + n1 + ” * ” + n1 + ” = ” + n1.multiply(n1));
System.out.println(“t” + n2 + ” * ” + n2 + ” = ” + n2.multiply(n2));
System.out.println(“t” + n1 + ” * ” + n2 + ” = ” + n1.multiply(n2));
System.out.println(“t” + n2 + ” * ” + n1 + ” = ” + n2.multiply(n1));
break;
case 4:
System.out.println(“t” + n1 + ” / ” + n1 + ” = ” + n1.divide(n1));
System.out.println(“t” + n2 + ” / ” + n2 + ” = ” + n2.divide(n2));
System.out.println(“t” + n1 + ” / ” + n2 + ” = ” + n1.divide(n2));
System.out.println(“t” + n2 + ” / ” + n1 + ” = ” + n2.divide(n1));
break;
case 5:
System.out.println(“t” + n1 + ” ct ” + n1 + ” = ” + n1.compareTo(n1));
System.out.println(“t” + n2 + ” ct ” + n2 + ” = ” + n2.compareTo(n2));
System.out.println(“t” + n1 + ” ct ” + n2 + ” = ” + n1.compareTo(n2));
System.out.println(“t” + n2 + ” ct ” + n1 + ” = ” + n2.compareTo(n1));
break;
case 6:
System.out.println(“t” + n1 + ” eq “+ n1 + ” = ” + n1.equals(n1));
System.out.println(“t” + n2 + ” eq “+ n2 + ” = ” + n2.equals(n2));
System.out.println(“t” + n1 + ” eq “+ n2 + ” = ” + n1.equals(n2));
System.out.println(“t” + n2 + ” eq “+ n1 + ” = ” + n2.equals(n1));
break;
case 7:
System.out.println(“t” + n1 + ” getReciprocal= ” + n1.getReciprocal());
break;
case 8:
System.out.println(“t” + n1 + ” toDouble = ” + n1.toDouble());
break;
case 9:
System.out.print( “read a fraction x/y, please enter x y : ” );
x = scanner.nextInt();
y = scanner.nextInt();
System.out.print(“t” + x +”/”+ y + ” setSimpleFraction = “);
n1.setSimpleFraction(x,y);
System.out.println(n1);
break;
}

} // end try   
catch ( SimpleFractionException fracitonException )
{
System.err.printf( “nFraction Exception: %sn”, fracitonException );
} // end catch   
} while ( continueLoop ); // end do…while   
} // end main
} // end class

/********************************************

/*********************************************'

public class SimpleFraction implements SimpleFractionInterface, Comparable
{
  
// integer numerator and denominator
private int num;
private int den;

public SimpleFraction()
{
setSimpleFraction(0,1);// default numbers
  
} // end of the default constructor

public SimpleFraction(int num, int den)
{
setSimpleFraction(num, den);//
} // end constructor

public void setSimpleFraction(int num, int den)
{
if (den == 0)
throw new SimpleFractionException(“denominator cannot be 0”);
else{
this.num= num;
this.den= den;
}
reduceSimpleFractionToLowestTerms();

} // end setSimpleFraction

public double toDouble()
{
  
// return double floating point value
return (double) num/den;
} // end toDouble

public SimpleFractionInterface add(SimpleFractionInterface secondFraction)
{
SimpleFraction secondFraction2;
secondFraction2 = (SimpleFraction) secondFraction;
// a/b + c/d is (ad + cb)/(bd)
int newNum = num*secondFraction2.getDen() + secondFraction2.getNum()*den;
int newDen = den*secondFraction2.getDen();
// return result which is a new SimpleFraction object
return new SimpleFraction(newNum,newDen);
} // end add

public SimpleFractionInterface subtract(SimpleFractionInterface secondFraction)
{
SimpleFraction secondFraction2;
secondFraction2 = (SimpleFraction) secondFraction;
// a/b – c/d is (ad – cb)/(bd)
int newNum = num*secondFraction2.getDen()-secondFraction2.getNum()*den;
int newDen = den*secondFraction2.getDen();
// return result which is a new SimpleFraction object
return new SimpleFraction(newNum,newDen);
} // end subtract

public SimpleFractionInterface multiply(SimpleFractionInterface secondFraction)
{
SimpleFraction secondFraction2;
secondFraction2 = (SimpleFraction) secondFraction;
// a/b * c/d is (ac)/(bd)
int newNum = num*secondFraction2.getNum();
int newDen = den*secondFraction2.getDen();
// return result which is a new SimpleFraction object
return new SimpleFraction(newNum,newDen);
} // end multiply

public SimpleFractionInterface divide(SimpleFractionInterface secondFraction)
{
SimpleFraction secondFraction2;
secondFraction2 = (SimpleFraction) secondFraction;
// a/b / c/d is (ad)/(bc)
// return SimpleFractionException if secondFraction is 0
if (secondFraction2.getNum() == 0)
throw new SimpleFractionException(“The second fraction cannot be 0”);
int newNum= num*secondFraction2.getDen();
int newDen= den*secondFraction2.getNum();
// return result which is a new SimpleFraction object
return new SimpleFraction(newNum,newDen);
} // end divide

public SimpleFractionInterface getReciprocal()
{
SimpleFraction r;
// return SimpleFractionException if secondFraction is 0
if(den == 0)
throw new SimpleFractionException(“The second fraction cannot be 0”);
else
r = new SimpleFraction(den,num);
// return result which is a new SimpleFraction object
return r;
} // end getReciprocal

public boolean equals(Object other)
{
boolean result = false;
if(other instanceof SimpleFraction){
SimpleFraction otherSimple = (SimpleFraction) other;

if(compareTo(otherSimple) == 0)
result = true;
}
return result;
} // end equals

public int compareTo(SimpleFraction other)
{
if(toDouble() == other.toDouble())
return 0;
else if(toDouble() > other.toDouble())
return 1;
else
return -1;
} // end compareTo

  
public String toString()
{
return num + “/” + den;
} // end toString

/** Task: Reduces a fraction to lowest terms. */
// private methods start here
private int getNum(){
return num;
}
  
private int getDen(){
return den;
}

private void reduceSimpleFractionToLowestTerms()
{
// Outline:
// compute GCD of num & den
// GCD works for + numbers.
// So, you should eliminate – sign
// then reduce numbers : num/GCD and den/GCD
int sign = 1; //1= positive numbers; -1 = negative numbers
if(num
sign = -1*sign;
num = num*-1;
}
if (den
sign= -1*sign;
den = den*-1;
}
int gcd= GCD(num,den);
num= (num/gcd)*sign;
den= den/gcd;
  
} // end reduceSimpleFractionToLowestTerms

/** Task: Computes the greatest common divisor of two integers.
* @param integerOne an integer
* @param integerTwo another integer
* @return the greatest common divisor of the two integers */
private int GCD(int integerOne, int integerTwo)
{
int result;

if (integerOne % integerTwo == 0)
result = integerTwo;
else
result = GCD(integerTwo, integerOne % integerTwo);

return result;
} // end GCD

//—————————————————————–
// Simple test is provided here

public static void main(String[] args)
{
SimpleFractionInterface firstOperand = null;
SimpleFractionInterface secondOperand = null;
SimpleFractionInterface result = null;
double doubleResult = 0.0;

SimpleFraction nineSixteenths = new SimpleFraction(9, 16); // 9/16
SimpleFraction oneFourth = new SimpleFraction(1, 4); // 1/4

System.out.println(“n=========================================n”);
// 7/8 + 9/16
firstOperand = new SimpleFraction(7, 8);
result = firstOperand.add(nineSixteenths);
System.out.println(“The sum of ” + firstOperand + ” and ” +
nineSixteenths + ” is tt” + result);
System.out.println(“tExpected result :tt23/16n”);

// 9/16 – 7/8
firstOperand = nineSixteenths;
secondOperand = new SimpleFraction(7, 8);
result = firstOperand.subtract(secondOperand);
System.out.println(“The difference of ” + firstOperand +
” and ” + secondOperand + ” is t” + result);
System.out.println(“tExpected result :tt-5/16n”);

// 15/-2 * 1/4
firstOperand = new SimpleFraction(15, -2);
result = firstOperand.multiply(oneFourth);
System.out.println(“The product of ” + firstOperand +
” and ” + oneFourth + ” is t” + result);
System.out.println(“tExpected result :tt-15/8n”);

// (-21/2) / (3/7)
firstOperand = new SimpleFraction(-21, 2);
secondOperand= new SimpleFraction(3, 7);
result = firstOperand.divide(secondOperand);
System.out.println(“The quotient of ” + firstOperand +
” and ” + secondOperand + ” is t” + result);
System.out.println(“tExpected result :tt-49/2n”);

// -21/2 + 7/8
firstOperand = new SimpleFraction(-21, 2);
secondOperand= new SimpleFraction(7, 8);
result = firstOperand.add(secondOperand);
System.out.println(“The sum of ” + firstOperand +
” and ” + secondOperand + ” is tt” + result);
System.out.println(“tExpected result :tt-77/8n”);

// 0/10, 5/(-15), (-22)/7
firstOperand = new SimpleFraction(0, 10);
doubleResult = firstOperand.toDouble();
System.out.println(“The double floating point value of ” + firstOperand + ” is t” + doubleResult);
System.out.println(“tExpected result ttt0.0n”);
firstOperand = new SimpleFraction(1, -3);
doubleResult = firstOperand.toDouble();
System.out.println(“The double floating point value of ” + firstOperand + ” is t” + doubleResult);
System.out.println(“tExpected result ttt-0.333333333…n”);
firstOperand = new SimpleFraction(-22, 7);
doubleResult = firstOperand.toDouble();
System.out.println(“The double floating point value of ” + firstOperand + ” is t” + doubleResult);
System.out.println(“tExpected result ttt-3.142857142857143”);
System.out.println(“n=========================================n”);
firstOperand = new SimpleFraction(-21, 2);
System.out.println(“First = ” + firstOperand);
// equality check
System.out.println(“check First equals First: “);
if (firstOperand.equals(firstOperand))
System.out.println(“Identity of fractions OK”);
else
System.out.println(“ERROR in identity of fractions”);

secondOperand = new SimpleFraction(-42, 4);
System.out.println(“nSecond = ” + secondOperand);
System.out.println(“check First equals Second: “);
if (firstOperand.equals(secondOperand))
System.out.println(“Equality of fractions OK”);
else
System.out.println(“ERROR in equality of fractions”);

// comparison check
SimpleFraction first = (SimpleFraction)firstOperand;
SimpleFraction second = (SimpleFraction)secondOperand;
  
System.out.println(“ncheck First compareTo Second: “);
if (first.compareTo(second) == 0)
System.out.println(“SimpleFractions == operator OK”);
else
System.out.println(“ERROR in fractions == operator”);

second = new SimpleFraction(7, 8);
System.out.println(“nSecond = ” + second);
System.out.println(“check First compareTo Second: “);
if (first.compareTo(second)
System.out.println(“SimpleFractions
else
System.out.println(“ERROR in fractions

System.out.println(“ncheck Second compareTo First: “);
if (second.compareTo(first) > 0)
System.out.println(“SimpleFractions > operator OK”);
else
System.out.println(“ERROR in fractions > operator”);

System.out.println(“n=========================================”);

System.out.println(“ncheck SimpleFractionException: 1/0”);
try {
SimpleFraction a1 = new SimpleFraction(1, 0);   
System.out.println(“Error! No SimpleFractionException”);
}
catch ( SimpleFractionException fe )
{
System.err.printf( “Exception: %sn”, fe );
} // end catch
System.out.println(“Expected result : SimpleFractionException!n”);

System.out.println(“ncheck SimpleFractionException: division”);
try {
SimpleFraction a2 = new SimpleFraction();   
SimpleFraction a3 = new SimpleFraction(1, 2);   
a3.divide(a2);
System.out.println(“Error! No SimpleFractionException”);
}
catch ( SimpleFractionException fe )
{
System.err.printf( “Exception: %sn”, fe );
} // end catch
System.out.println(“Expected result : SimpleFractionException!n”);

} // end main
} // end SimpleFraction

/************************************************

/**********************************************

public interface SimpleFractionInterface
{
/** Task: Sets a fraction to a given value.
* @param num is the integer numerator
* @param den is the integer denominator
* @throw ArithmeticException if denominator is 0 */
public void setSimpleFraction(int num, int den);

/** Task: convert a fraction to double value
* @return the double floating point value of a fraction */
public double toDouble();

/** Task: Adds two fractions.
* @param secondFraction is a fraction that is the second operand of the addition
* @return a fraction which is the sum of the invoking fraction and the secondFraction */
public SimpleFractionInterface add(SimpleFractionInterface secondFraction);

/** Task: Subtracts two fractions.
* @param secondFraction a fraction that is the second operand of the subtraction
* @return a fraction which is the difference of the invoking fraction and the second operand */
public SimpleFractionInterface subtract(SimpleFractionInterface secondFraction);

/** Task: Multiplies two fractions.
* @param secondFraction a fraction that is the second operand of the multiplication
* @return a fraction which is the product of the invoking fraction and the secondFraction*/
public SimpleFractionInterface multiply(SimpleFractionInterface secondFraction);

/** Task: Divides two fractions.
* @param secondFraction a fraction that is the second operand of the division
* @return a fraction which the quotient of the invoking fraction and the secondFraction
* @throw FractionException if secondFraction is 0 */
public SimpleFractionInterface divide(SimpleFractionInterface secondFraction);

/** Task: Get's the fraction's reciprocal
* @return the reciprocal of the invoking fraction
* @throw FractionException if the new number with denominator is 0*/
public SimpleFractionInterface getReciprocal();

}

/*******************************************************

/****************************************************

public class SimpleFractionException extends RuntimeException
{
public SimpleFractionException()
{
this(“”);
}

public SimpleFractionException(String errorMsg)
{
super(errorMsg);
}

}

/***************************************************

/*************************************************

Thanks so much I look forward to reading the responses to this and running the GUI.

"Get 15% discount on your first 3 orders with us"
Use the following coupon
"FIRST15"

Order Now