Computer Science Unit 3 – GUIs and I/O

Creating your project and programs for Unit 3 – Lab07 GCD and LCM

Greatest Common Divisor and Least Common Multiple

We're saving our programs for Unit 3 in separate folders... Lab00, Lab01, Lab02 folders, etc. We'll use 'packages' to create these new folders for our separate programs.

1. Lab07 GCD and LCM, see Packets: Unit3 GUIs and I/O page 27

3. Create Driver07.java, Panel07.java, Display07.java

Right click on Unit3Progs: Source Packages

New – Java class

Class Name: Driver07

Project: Unit3Progs

Location: Source Packages

Package: Lab07

Right click on Unit3Progs: Source Packages

New – Java class

Class Name: Panel07

Project: Unit3Progs

Location: Source Packages

Package: Lab07

Right click on Unit3Progs: Source Packages

New – Java class

Class Name: Display07

Project: Unit3Progs

Location: Source Packages

Package: Lab07

4. Type the code for Driver07, Panel07, and Display07

package Lab07;

import javax.swing.JFrame;

public class Driver07

{

public static void main(String[] args)

{

JFrame frame = new JFrame("GCD and LCM");

frame.setSize(220, 200);

frame.setLocation(200, 100);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setContentPane(______);

frame.setVisible(_____);

}

}

5.

package Lab07; // NOTE: PANEL07 IS NOT INCLUDED IN THE STUDENT FOLDER,

// ONLY DISPLAY07SHELL is given

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class Panel07 extends ______

{

private Display07 display;

public Panel07()

{

setLayout(new FlowLayout());

display = new ______();

add(______);

JButton button1 = new JButton("GCD");

button1.______(new Listener1());

add(______);

JButton button2 = new JButton("LCM");

button2.______(new Listener2());

add(button2);

}

private class Listener1 implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

display.showGCD();

}

}

private class Listener2 implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

______();

}

}

}

6.

package Lab07;

import javax.swing.*;

import java.awt.*;

public class Display07 extends JPanel

{

private JLabel label;

private JTextField box1, box2;

public Display07()

{

setLayout(new FlowLayout());

setPreferredSize(new Dimension(200, 125));

JPanel subpanel = new JPanel();

subpanel.setLayout(new FlowLayout());

subpanel.add(new JLabel("One: "));

box1 = new JTextField("", 5);

box1.setHorizontalAlignment(SwingConstants.CENTER);

subpanel.add(box1);

subpanel.add(new JLabel("Two: "));

box2 = new JTextField("", 5);

box2.setHorizontalAlignment(SwingConstants.CENTER);

subpanel.add(box2);

add(subpanel);

label = new JLabel("?");

label.setFont(new Font("Serif", Font.BOLD, 75));

label.setForeground(Color.blue);

add(label);

}

public void showGCD()

{

int x = Integer.parseInt(box1.getText());

int y = Integer.parseInt(box2.getText());

int z = gcd(x, y);

label.setText("" + z);

}

public void showLCM()

{

int x = Integer.parseInt(box1.getText());

int y = Integer.parseInt(box2.getText());

int z = x * y / gcd(x, y);

label.setText("" + z);

}

private int gcd(int a, int b)

{

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

/* */

/* Your code goes here. */

/* */

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

int n = Math._____(______, ______); //n= the minimum of the absolute values of a, b

while(!(a % n == 0 & b % n == 0)) // while the remainders of dividing a and b by n == 0

n = ______; // reduce n by 1

return ___; // return the last n that had 0 remainder when divided into a and b

}

}