Module: CSY1020 Problem Solving & Programming

Published: 2023-01-24
Module:     CSY1020 Problem Solving & Programming
Type of paper:  Report
Categories: Psychology United States Accounting
Pages: 4
Wordcount: 1070 words
9 min read
143 views

Tutor: Gary Hill

Is your time best spent reading someone else’s essay? Get a 100% original essay FROM A CERTIFIED WRITER!

@version: 1.0

Date: 11/06/19

*/

import java.awt.*;

import javax.swing.*;

public class Exercise3_3 extends JFrame{

//Named constants for the game board

public static final int ROWS =3;

public static final int COLS =3;

//constants for the board dimensions

public static final int CELL_SIZE = 100;

// cell width and height (square)

public static final int CANVAS_WIDTH = CELL_SIZE * COLS;

// the drawing canvas

public static final int CANVAS_HEIGHT = CELL_SIZE * ROWS;

public static final int GRID_WIDTH = 8;

// Grid-line's width

public static final int GRID_WIDTH_HALF = GRID_WIDTH / 2;

// Grid-line's half-width

// Symbols (cross/nought) are displayed inside a cell, with padding from border

public static final int CELL_PADDING = CELL_SIZE / 6;

public static final int SYMBOL_SIZE = CELL_SIZE - CELL_PADDING * 2;

// width/height

public static final int SYMBOL_STROKE_WIDTH = 8;

//enum type either an empty, a cross or a nought

public enum Seed { EMPTY, CROSS, NOUGHT }

private Seed[][] board ;

private DrawCanvas canvas;

public Exercise3_3() {

//instantizte the inner class

canvas = new DrawCanvas();

canvas.setPreferredSize(new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT));

repaint();

//initialize the container to house the Board

Container cp = getContentPane();

cp.setLayout(new BorderLayout());

cp.add(canvas, BorderLayout.CENTER);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

pack(); //pack all the components in this JFrame

setTitle("Tic Tac Toe");

setVisible(true);

board = new Seed[ROWS][COLS];

//call initGame() to empty the grid

initGame();

//Some initial values are set after the grid is drawn

board[1][2] = Seed.CROSS;

board[2][1] = Seed.NOUGHT;

board[1][0] = Seed.CROSS;

board[1][1] = Seed.NOUGHT;

board[0][1] = Seed.CROSS;

}

public void initGame() {

for (int row=0; row<ROWS; ++row) {

for (int col = 0; col<COLS; ++col) {

board[row][col] = Seed.EMPTY;

}

}

}

//inner class so as to be able to use the Graphics object

class DrawCanvas extends JPanel{

@Override

public void paintComponent(Graphics g) {

super.paintComponent(g);

setBackground(Color.WHITE);

//Draw the grid lines

g.setColor(Color.LIGHT_GRAY);

for(int row = 1; row<ROWS; ++row) {

g.fillRoundRect(0, CELL_SIZE*row - GRID_WIDTH_HALF, CANVAS_WIDTH-1 ,

GRID_WIDTH, GRID_WIDTH, GRID_WIDTH);

}

for (int col =1 ; col<COLS; ++col) {

g.fillRoundRect(CELL_SIZE*col - GRID_WIDTH_HALF,0,GRID_WIDTH, CANVAS_HEIGHT-1, GRID_WIDTH, GRID_WIDTH);

}

// Draw the Seeds of all the cells if they are not empty

// Use Graphics2D which allows us to set the pen's stroke

Graphics2D g2d = (Graphics2D)g;

g2d.setStroke(new BasicStroke(SYMBOL_STROKE_WIDTH,

BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)); // Graphics2D only

for (int row = 0; row < ROWS; ++row) {

for (int col = 0; col < COLS; ++col) {

int x1 = col * CELL_SIZE + CELL_PADDING;

int y1 = row * CELL_SIZE + CELL_PADDING;

if (board[row][col] == Seed.CROSS) {

g2d.setColor(Color.RED);

int x2 = (col + 1) * CELL_SIZE - CELL_PADDING;

int y2 = (row + 1) * CELL_SIZE - CELL_PADDING;

g2d.drawLine(x1, y1, x2, y2);

g2d.drawLine(x2, y1, x1, y2);

} else if (board[row][col] == Seed.NOUGHT) {

g2d.setColor(Color.BLACK);

g2d.drawOval(x1, y1, SYMBOL_SIZE, SYMBOL_SIZE);

}

}

}

}

}

public static void main(String[] args) {

// TODO Auto-generated method stub

SwingUtilities.invokeLater(new Runnable() {

@Override

public void run() {

new Exercise3_3();

}

});

}

}

Screenshots

Exercise: 3.4: Design a simple house and draw it.Source Codepackage exercises;

/** Exercise: 3.4: Design a simple house and draw it.

Filename: Exercise3_4.java

@author: A Gary Hill (200WXYZ)

Course: BSc Computing

Module: CSY1020 Problem Solving & Programming

Tutor: Gary Hill

@version: 1.0

Date: 11/06/19

*/

import java.awt.*;

import javax.swing.*;

import java.applet.*;

public class Exercise3_4 extends Applet{

public void paint(Graphics g) {

int a[]= {150,300,225};

int b[]= {150,150,25};

g.setColor(Color.blue);

g.fillRect(150, 150, 150, 200);

g.setColor(Color.black);

g.fillRect(190, 220, 70, 130);

g.setColor(Color.red);

g.fillPolygon(a, b, 3);

g.setColor(Color.yellow);

g.fillOval(200, 75, 50, 50);

g.setColor(Color.red);

g.fillRect(300, 150, 250, 200);

g.setColor(Color.black);

g.fillRect(390, 210, 80, 80);

//The Window partitions

g.setColor(Color.blue);

g.drawLine(430, 210, 430, 290);

g.setColor(Color.blue);

g.drawLine(470, 250, 390, 250);

g.setColor(Color.red);

g.drawLine(460, 26, 226, 26);

g.setColor(Color.red);

g.drawLine(460, 25, 550, 150);

}

public static void main(String[] args) {

//create a jframe to house the applet

JFrame jp1 = new JFrame();

//instatiated the class

Exercise3_4 exe = new Exercise3_4();

jp1.getContentPane().add(exe, BorderLayout.CENTER);

jp1.setSize(new Dimension(700,500));

jp1.setVisible(true);

}

}

Screen Shots

Exercise: 4.1This exercise was fairly straightforward and thus was implemented via a console program that simply prompts the user to enter the three dimensions and from that it computes the volume by multiplying the inputs together.

Source Code/** Exercise: 4.1: Write a program to compute the volume of a box, given its three dimensions.

Filename: Exercise4_1.java

@author: A Gary Hill (200WXYZ)

Course: BSc Computing

Module: CSY1020 Problem Solving & Programming

Tutor: Gary Hill

@version: 1.0

Date: 11/06/19

*/

package exercises;

import java.util.Scanner;

public class Exercise4_1{

public static void main(String []args){

Scanner scan = new Scanner(System.in);

int dim1, dim2, dim3, volume;

System.out.println("Enter the three dimensions of the box");

//read the values entered into the console into the variables

dim1 = scan.nextInt();

dim2 = scan.nextInt();

dim3 = scan.nextInt();

volume = dim1*dim2*dim3;

//print to the console

System.out.println(String.format("Volume = %d", volume));

}

}

Screenshots

Exercise: 4.3This exercise was also implemented as a console program. The program begins by prompting the user to enter three integer exam marks, the program then evaluates the average in double. The challenge with the program is that the number is not rounded off before being cast thus there is some discrepancy between the value obtained by a calculator versus the program output. Attempts were made to rectify this but without changing the type, there was no other obvious solution.

Source Codepackage exercises;

/** Exercise: 4.3: Write a program that inputs three integer exam marks,

which displays the mean (average) mark as a double value.

Check your answer with a calculator.

Filename: Exercise4_3.java

@author: A Gary Hill (200WXYZ)

Course: BSc Computing

Module: CSY1020 Problem Solving & Programming

Tutor: Gary Hill

@version: 1.0

Date: 11/06/19

*/

import java.util.Scanner;

import java.lang.Math;

public class Exercise4_3 {

public static void main(String []args){

Scanner scan = new Scanner(System.in);

int mark1, mark2, mark3;

double mean;

//prompts the user to enter the marks

System.out.println("Enter the three marks");

//takes values from the console and puts them into the variables

mark1 = scan.nextInt();

mark2 = scan.nextInt();

mark3 = scan.nextInt();

mean = Math.round(((mark1+mark2+mark3)/3));

//Average is printed into the console

System.out.println("Average = " + mean);

}

}

Screenshots

Exercise: 5.3The program engages the user using dialog boxes presenting a more user-friendly interface.

Source Codepackage exercises;

/** Exercise: 5.3: write a method named displayEarnings with two integer

parameters representing an employeeaEURs salary and the number of years they have worked.

The method should display their total earnings in a message dialog,

assuming that they earned the same amount every year.

The program should obtain values via input dialogs prior to calling displayEarnings

Filename: Exercise5_3.java

@author: A Gary Hill (200WXYZ)

Course: BSc Computing

Module: CSY1020 Problem Solving & Programming

Tutor: Gary Hill

@version: 1.0

Dat...

Cite this page

Module: CSY1020 Problem Solving & Programming. (2023, Jan 24). Retrieved from https://speedypaper.com/essays/module-csy1020-problem-solving-amp-programming

Request Removal

If you are the original author of this essay and no longer wish to have it published on the SpeedyPaper website, please click below to request its removal:

didn't find image

Liked this essay sample but need an original one?

Hire a professional with VAST experience!

24/7 online support

NO plagiarism