Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

COMP163 Flowchart Programs For this assignment, you are to write three Java prog

ID: 3889647 • Letter: C

Question

COMP163 Flowchart Programs For this assignment, you are to write three Java programs using flowcharts as a guide. Note that the flowcharts have different syntax than Java. For example, variables, classes and methods are not declared in the flowcharts. You will have to convert the flowchart to a working Java program. When you have written and tested your programs, upl load the Java files to Blackboard. Problem 2 should be a command line Java application. Either problem 1 or problem 3 must be written with a Graphical User Interface. For GUI programs, the flowchart should be implemented in the ActionPerformed method 1. Write a Java program that determines if a given number is a prime start Example output Enter a number 143 not prime read num true num%2 = 0? print "not prime" Enter a number 127 prime false sqrt = Vnum check - 3 Enter a number check

Explanation / Answer

1) Prime number check

package primenumber;

import java.awt.*; // Using AWT's containers and components

import java.awt.event.*; // Using AWT's event classes and listener interfaces

public class prime1 extends Frame implements ActionListener{

// An AWT GUI program inherits the top-level container java.awt.Frame

private Label lblCount;

private Label lblCount1;

private Label lblCount2;

// Declare component Label

private TextField tfCount; // Declare component TextField

private Button btnCount; // Declare component Button

private int count = 0; // counter's value

// Constructor to setup UI components and event handlers

public prime1 () {

setLayout(new FlowLayout());

// "super" Frame sets layout to FlowLayout, which arranges

// Components from left-to-right, then top-to-bottom.

lblCount = new Label("Enter a Number"); // Construct component Label

add(lblCount); // "super" Frame adds Label

tfCount = new TextField(count + "", 15); // Construct component TextField

tfCount.setEditable(true); // read-only

add(tfCount); // "super" Frame adds TextField

btnCount = new Button("Check"); // Construct component Button

add(btnCount); // "super" Frame adds Button

btnCount.addActionListener(this);

// btnCount is the source object that fires ActionEvent when clicked.

// The source add "this" instance as an ActionEvent listener, which provides

// an ActionEvent handler called actionPerformed().

// Clicking btnCount invokes actionPerformed().

setSize(350, 200); // "super" Frame sets initial size

setTitle("Prime Number Check"); // "super" Frame sets title

setVisible(true); // show "super" Frame

addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent we)

{

System.exit(0);

}

});

}

// ActionEvent handler - Called back when the button is clicked.

@Override

public void actionPerformed(ActionEvent evt) {

int n=Integer.parseInt(tfCount.getText());

System.out.println(n);

boolean flag1=true;

if (n%2==0) {

flag1=false;

}

for(int i=3;i*i<=n;i+=2) {

if(n%i==0)

flag1=false;

}

if(flag1)

{

lblCount1 = new Label("Number is prime"); // Construct component Label

add(lblCount1);

setLayout(new FlowLayout());

setVisible(true);

addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent we)

{

System.exit(0);

}

});// show "super" Frame

}

else {

lblCount2 = new Label("Number is not prime"); // Construct component Label

add(lblCount2);

setLayout(new FlowLayout());

setVisible(true); // show "super" Frame

addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent we)

{

System.exit(0);

}

});

}

  

}

// The entry main() method

public static void main(String[] args) {

// Invoke the constructor by allocating an anonymous instance

new prime1();

}

}

2) hailstone value of integer

package primenumber;

import java.util.Scanner;

public class hailstone {

public static void main(String[] args) {

Scanner inputScanner = new Scanner(System.in);

System.out.printf("Enter a Number: ");

try

{

int number = inputScanner.nextInt();

while (number !=1) {

if (number % 2 == 0) {

System.out.println( number / 2);

number /= 2;

}

else {

System.out.println((number * 3 + 1));

number = number * 3 + 1;

}

}

}

catch (Exception e) {

System.out.println("There is some exception");

}

}

}

3) GCD of two numbers

package primenumber;

import java.awt.*; // Using AWT's containers and components

import java.awt.event.*; // Using AWT's event classes and listener interfaces

public class prime1 extends Frame implements ActionListener{

// An AWT GUI program inherits the top-level container java.awt.Frame

private Label lblCount;

private Label lblCount1;

private Label lblCount2;

private Label lblCount3;

// Declare component Label

private TextField tfCount1;

private TextField tfCount; // Declare component TextField

private Button btnCount; // Declare component Button

private int count = 0; // counter's value

// Constructor to setup UI components and event handlers

public prime1 () {

setLayout(new FlowLayout());

// "super" Frame sets layout to FlowLayout, which arranges

// Components from left-to-right, then top-to-bottom.

lblCount = new Label("Enter first Number"); // Construct component Label

add(lblCount);

// "super" Frame adds Label

  

lblCount3 = new Label("Enter second Number"); // Construct component Label

add(lblCount3);

tfCount = new TextField(count + "", 15); // Construct component TextField

tfCount.setEditable(true); // read-only

add(tfCount);

// "super" Frame adds TextField

tfCount1 = new TextField(count + "", 15); // Construct component TextField

tfCount1.setEditable(true); // read-only

add(tfCount1);   

  

btnCount = new Button("Calculate"); // Construct component Button

add(btnCount); // "super" Frame adds Button

btnCount.addActionListener(this);

// btnCount is the source object that fires ActionEvent when clicked.

// The source add "this" instance as an ActionEvent listener, which provides

// an ActionEvent handler called actionPerformed().

// Clicking btnCount invokes actionPerformed().

setSize(350, 200); // "super" Frame sets initial size

setTitle("Calculate GCD of two Numbers"); // "super" Frame sets title

setVisible(true); // show "super" Frame

addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent we)

{

System.exit(0);

}

});

}

// ActionEvent handler - Called back when the button is clicked.

@Override

public void actionPerformed(ActionEvent evt) {

int n1=Integer.parseInt(tfCount.getText());

int n2=Integer.parseInt(tfCount1.getText());

while(n1 != n2)

{

if(n1 > n2)

n1 = n1-n2;

else

n2 = n2-n1;

}

String s=("GCD of numbers is"+ String.valueOf(n1));

lblCount1 = new Label(s); // Construct component Label

add(lblCount1);

setLayout(new FlowLayout());

setVisible(true);

addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent we)

{

System.exit(0);

}

});// show "super" Frame

}

  

// The entry main() method

public static void main(String[] args) {

// Invoke the constructor by allocating an anonymous instance

new prime1();

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote