Professional Documents
Culture Documents
Bsc/Beng/Hnd Computing (Year 1)
Bsc/Beng/Hnd Computing (Year 1)
Bsc/Beng/Hnd Computing (Year 1)
(Year 1)
1
CSY1020: Problem Solving Programming
Assignment 2: Programming (Java) (50%)
19436768
BSc Computing
University of Northampton
Submission date:
2
Contents
1. Introduction…………………………………………………………………………………………….. 4
2. Exercises……………………………………………………………………………………………………5
3. Conclusion…………………………………………………………………………………………………36
4. References…………………………………………………………………………………………………..37
3
Introduction
The assignment for problem solving and programming with 50% weightage has been coded
entirely in Java. According to (Eck and Colleges, 2010), Java is a high-level programming
language which is based on Class, Object, Structure etc. Though, Java is not a pure object-
oriented language, as it contains primitive type and developed to have as limited execution
dependencies as possible (Larsen, 2001). Spring Tool Suite 3.99 was as an IDE and the system
on which the author coded these programs was running JDK version 1.8.0_211 (Loo, 2007). The
programming code includes the thorough concept of Object-oriented programming that
includes its major characteristics like Encapsulation, Polymorphism, Interfaces, Inheritance etc.
(Huntley et al., 2019). The programs illustrated below are either console-based or GUI-based
(Murrell, 2008). The code and implementation parts shed some more individual lights for each
exercise addressed.
4
Exercises
Exercise: 3.3: Draw an empty Tic Tac Toe (noughts and crosses) board.
Code:
/**
Exercise: Draw an empty Tic Tac Toe (noughts and crosses) board.
Filename: Exercise3_3.java
@author:
Course: BSc Computing
Module: CSY1020 Problem Solving & Programming
Tutor:
@version: 1.0
Date:
*/
public class Exercise3_3 {
}
}
Implementation: The above code draws a tic tac toe board using a for loop. It runs on
console.
Code:
/**
Exercise: Design a simple house and draw it.
Filename: Exercise3_4.java
@author:
Course: BSc Computing
Module: CSY1020 Problem Solving & Programming
Tutor:
@version: 1.0
Date:
*/
import java.awt.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class Exercise3_4 extends JPanel{
5
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.WHITE);
g.setColor(Color.RED);
g.fillRect(150,200, 160, 100);
g.setColor(Color.GREEN);
g.fillRect(150,100, 160, 100);
g.setColor(Color.BLACK);
g.fillRect(150,0, 160, 100);
g.setColor(Color.BLUE);
g.fillRect(150, 300, 160, 100);
g.setColor(Color.YELLOW);
g.fillRect(215, 360, 20, 40);
6
Exercise: 4.1: Write a program to compute the volume of a box, given its three dimensions.
Code:
/**
Exercise: Write a program to compute the volume of a box, given its three dimensions.
Filename: Exercise4_1.java
@author:
Course: BSc Computing
Module: CSY1020 Problem Solving & Programming
Tutor:
@version: 1.0
Date:
*/
import java.util.Scanner;
Implementation: The above code takes three input from user for length breadth and
height of a box and displays its volume to the user. This is console-based.
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.
7
Code:
/**
Exercise: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:
Course: BSc Computing
Module: CSY1020 Problem Solving & Programming
Tutor:
@version: 1.0
Date:
*/
import java.util.Scanner;
Exercise: 5.3: write a method named displayEarnings with two integer parameters
representing an employee’s 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.
Code:
/**
Exercise: write a method named displayEarnings with two integer parameters
representing an
8
employee’s 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:
Course: BSc Computing
Module: CSY1020 Problem Solving & Programming
Tutor:
@version: 1.0
Date:
*/
import javax.swing.JFrame;
import javax.swing.JOptionPane;
@SuppressWarnings("serial")
public class Exercise5_3 extends JFrame {
Implementation: The above code takes two integer parameters, salary and years
respectively and displays the total earnings. All of this is implemented through the use of
JOptionPane.
Exercise 6. 2. Write a program which produces a random number between 200 and 400
each time a button is clicked. The program should display this number, and the sum and
average of all the numbers so far. As you click again and again, the average should
converge on 300. If it doesn't, we would suspect the random number generator - just as we
would be suspicious of a coin that came out heads 100 times in a row!
Code:
/**
9
Exercise: Write a program which produces a random number between 200 and 400 each
time
a button is clicked. The program should display this number, and the sum and average
of all the
numbers so far. As you click again and again, the average should converge on 300. If
it doesn't, we
would suspect the random number generator - just as we would be suspicious of a coin
that came
out heads 100 times in a row!
Filename: Exercise6_2.java
@author:
Course: BSc Computing
Module: CSY1020 Problem Solving & Programming
Tutor:
@version: 1.0
Date:
*/
import java.awt.EventQueue;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
@SuppressWarnings("serial")
public class Exercise6_2 extends JFrame {
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Exercise6_2 frame = new Exercise6_2();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
10
}
});
}
/**
* Create the frame.
*/
public Exercise6_2() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
contentPane.add(getBtnNewButton());
contentPane.add(number());
contentPane.add(sum());
contentPane.add(getSumval());
contentPane.add(getAverage());
contentPane.add(getAverageval());
}
count++;
int n = r.nextInt(200) + 200;
s += n;
avg = s / count;
number.setText(String.valueOf(n));
Sumval.setText(String.valueOf(s));
Averageval.setText(String.valueOf(avg));
11
}
Implementation: The above code was implemented by extending J Frame class. This
program can generate a random number between 200 and 400 with the click of a button.
Additionally, it also has two features; sum and average. The sum feature displays the total
value of all the generated numbers added together in a particular session, and the average
feature displays the average value of all the numbers generated during that particular
session.
Exercise 7.4 Betting A group of people are betting on the outcome of three throws of the
dice. A person bets $1 on predicting the outcome of the three throws. Write a program that
uses the random number method to simulate three throws of a die and displays the
winnings according to the following rules:
• All three throws are sixes: win $20;
• All three throws are the same (but not sixes): win $10;
• Any two of the three throws are the same: win $5.
Code:
/**
12
Exercise: Betting A group of people are betting on the outcome of three throws of the
dice. A
person bets $1 on predicting the outcome of the three throws. Write a program that
uses the
random number method to simulate three throws of a die and displays the winnings
according to the
following rules:
• all three throws are sixes: win $20;
• all three throws are the same (but not sixes): win $10;
• any two of the three throws are the same: win $5.
Filename: Exercise7_4.java
@author:
Course: BSc Computing
Module: CSY1020 Problem Solving & Programming
Tutor:
@version: 1.0
Date:
*/
import java.util.Random;
else {
System.out.println("Sorry, no luck this time. You lost 1$");
}
13
Implementation: This program uses Random class to generate 3 random numbers
between 1 and 6 (including them) and based on the results and the rules of the question
displays the winnings to the user.
Exercise: 8.1: Write a program that uses a loop to display the integer numbers 1 to 10
together with the cubes of each of their values.
Code:
/**
Exercise: Write a program that uses a loop to display the integer numbers 1 to 10
together with the cubes of each of their values.
Filename: Exercise8_1.java
@author:
Course: BSc Computing
Module: CSY1020 Problem Solving & Programming
Tutor:
@version: 1.0
Date:
*/
}
}
Implementation: This code prints 10 consecutive numbers starting from 1, along with
their respective cubes. Math.pow() function was used to achieve the cubes. The results are
printed in the console.
Exercise 8.2: Random Numbers. Write a program to display 10 random numbers using a
loop. Use
the library class Random to obtain integer random numbers in the range 0 to 9. Display the
numbers in a text field.
Code:
/**
Exercise: Random Numbers. Write a program to display 10 random numbers using a loop.
Use the library class Random to obtain integer random numbers in the range 0 to 9.
Display the numbers in a text field.
Filename: Exercise8_2.java
@author:
Course: BSc Computing
Module: CSY1020 Problem Solving & Programming
Tutor:
@version: 1.0
14
Date:
*/
import java.awt.EventQueue;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;
@SuppressWarnings("serial")
public class Exercise8_2 extends JFrame {
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Exercise8_2 frame = new Exercise8_2();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
/**
* Create the frame.
*/
public Exercise8_2() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 521, 383);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
contentPane.add(getTextArea());
}
15
textArea = new JTextArea(String.valueOf(randomNumber()) + "\n\n"
+ String.valueOf(randomNumber()) + "\n\n"
+ String.valueOf(randomNumber()) + "\n\n" +
String.valueOf(randomNumber()) + "\n\n"
+ String.valueOf(randomNumber()) + "\n\n" +
String.valueOf(randomNumber()) + "\n\n"
+ String.valueOf(randomNumber()) + "\n\n" +
String.valueOf(randomNumber()) + "\n\n"
+ String.valueOf(randomNumber()) + "\n\n" +
String.valueOf(randomNumber()));
textArea.setBounds(61, 11, 336, 322);
textArea.setColumns(10);
}
return textArea;
}
}
Implementation: The above code uses the Random class to generate random numbers
between 0-9 and display them in a text field.
Exercise 9.3: Bank account Write a program that simulates a bank account. A button
allows a deposit to be made into the account. The amount is entered into a text field. A
second button allows a withdrawal to be made. The amount (the balance) and the state of
the account is continually displayed - it is either OK or overdrawn. Create a class named
Account to represent bank accounts. It has methods deposit, withdraw,
getCurrentBalance and setCurrentbalance.
Code:
/**
Exercise: Bank account Write a program that simulates a bank account. A button allows
a deposit to be
made into the account. The amount is entered into a text field. A second button
allows a withdrawal
to be made. The amount (the balance) and the state of the account is continually
displayed - it is
either OK or overdrawn. Create a class named Account to represent bank accounts. It
has methods
deposit, withdraw, getCurrentBalance and setCurrentbalance.
Filename: Exercise9_3.java
@author:
Course: BSc Computing
Module: CSY1020 Problem Solving & Programming
Tutor:
@version: 1.0
Date:
*/
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
16
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
@SuppressWarnings("serial")
public class Exercise9_3 extends JFrame {
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Exercise9_3 frame = new Exercise9_3();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Exercise9_3() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
contentPane.add(getBtnWithdraw());
contentPane.add(getBtnDeposit());
contentPane.add(getWithdrawAmt());
contentPane.add(getDepositAmt());
contentPane.add(getLblNewLabel());
contentPane.add(getLblCurrentbalence());
contentPane.add(getLblStatus());
contentPane.add(getLblAccountstatus());
}
17
private JButton getBtnWithdraw() {
if (btnWithdraw == null) {
btnWithdraw = new JButton("Withdraw");
btnWithdraw.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
double amt = Double.valueOf(withdrawAmt.getText());
a.withdraw(amt);
lblCurrentbalence.setText(String.valueOf(a.getCurrentBalance()));
if (a.getCurrentBalance() > 0)
lblAccountstatus.setText("OK");
else
lblAccountstatus.setText("Overdrawn");
withdrawAmt.setText(null);
}
});
btnWithdraw.setBounds(205, 56, 89, 23);
}
return btnWithdraw;
}
lblCurrentbalence.setText(String.valueOf(a.getCurrentBalance()));
if (a.getCurrentBalance() > 0)
lblAccountstatus.setText("OK");
else
lblAccountstatus.setText("Overdrawn");
depositAmt.setText(null);
}
});
btnDeposit.setBounds(205, 102, 89, 23);
}
return btnDeposit;
}
18
depositAmt.setBounds(39, 103, 86, 20);
depositAmt.setColumns(10);
}
return depositAmt;
}
class Account {
private double currentBalance = 0;
19
this.currentBalance = currentBalance;
}
Implementation: The above code has a class named Account with the required methods
that are used in the GUI to withdraw, deposit, show current balance and show current
status of a bank account. Action listeners were added to the buttons to make them function
as was desired.
Exercise 9.4:
Scorekeeper Design and write a class that acts as a scorekeeper for a computer game. It
maintains a single integer, the score. It provides a method to initialize the score to zero, a
method to increase the score, a method to decrease the score, and a method to return the
score. Write a program to create a single object and use it. The current score is always on
display in a text field. Buttons are provided to increase, decrease and initialize the score by
an amount entered into a text field.
Code:
/**
Exercise: Scorekeeper Design and write a class that acts as a scorekeeper for a
computer game. It maintains
a single integer, the score. It provides a method to initialize the score to zero, a
method to increase
the score, a method to decrease the score, and a method to return the score. Write a
program to
create a single object and use it. The current score is always on display in a text
field. Buttons are
provided to increase, decrease and initialize the score by an amount entered into a
text field.
Filename: Exercise9_4.java
@author:
Course: BSc Computing
Module: CSY1020 Problem Solving & Programming
Tutor:
@version: 1.0
Date:
*/
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
@SuppressWarnings("serial")
20
public class Exercise9_4 extends JFrame {
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Exercise9_4 frame = new Exercise9_4();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Exercise9_4() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
contentPane.add(getLblCurrentScore());
contentPane.add(getLblScore());
contentPane.add(getBtnIncreaseBy());
contentPane.add(getBtnDecreaseBy());
contentPane.add(getBtnInitializeTo());
contentPane.add(getTxtInitialize());
contentPane.add(getTxtIncrease());
contentPane.add(getTxtDecrease());
contentPane.add(getBtnZero());
}
21
lblCurrentScore.setBounds(29, 38, 84, 14);
}
return lblCurrentScore;
}
22
});
btnInitializeTo.setBounds(29, 79, 119, 23);
}
return btnInitializeTo;
}
class ScoreKeeper {
private int score;
23
setScore(0);
}
Implementation: The above code has an integer that can be manipulated using buttons to
initialize, increase or decrease it to the desired value. This program also makes use of two
classes, one that maintains the score and another that runs the GUI. The user can input
numbers in the fields provided to manipulate the score as they desire.
Exercise 11.1: Cost of phone call. A phone call costs 10 cents per minute. Write a program
that inputs via text fields the duration of a phone call, expressed in hours, minutes and
seconds, and displays the cost of the phone call in cents, accurate to the nearest cent.
Code:
/**
Exercise: Cost of phone call. A phone call costs 10 cents per minute.
Write a program that inputs via text fields the duration of a phone call, expressed
in hours, minutes
and seconds, and displays the cost of the phone call in cents, accurate to the
nearest cent.
Filename: Exercise11_1.java
@author:
Course: BSc Computing
Module: CSY1020 Problem Solving & Programming
Tutor:
@version: 1.0
Date:
*/
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
24
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
@SuppressWarnings("serial")
public class Exercise11_1 extends JFrame {
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Exercise11_1 frame = new Exercise11_1();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Exercise11_1() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
contentPane.add(getLblTimeOfCall());
contentPane.add(getLblHours());
contentPane.add(getLblMinutes());
contentPane.add(getLblSeconds());
contentPane.add(getTxtHrs());
contentPane.add(getTxtMins());
contentPane.add(getTxtSecs());
contentPane.add(getBtnCalculateCost());
contentPane.add(getLblCallcost());
contentPane.add(getLblChargeRate());
25
}
26
txtMins.setText("0");
txtMins.setBounds(78, 114, 86, 20);
txtMins.setColumns(10);
}
return txtMins;
}
Implementation: The above code is also made in swing. The user can input time in three
formats, hours, minutes and seconds. A button is provided which calculates the total cost of
the call and displays it to the user.
27
Exercise 11.2: Measurement Conversion. Write a program to input a measurement
expressed in feet and inches via two text fields. When a button is clicked, convert the
measurement to centimeters and display it in a text field, correct to two decimal places.
There are 12 inches in a foot; 1 inch is 2.54 centimeters.
Code:
/**
Exercise: Measurement Conversion. Write a program to input a measurement expressed in
feet
and inches via two text fields. When a button is clicked, convert the measurement to
centimetres
and display it in a text field, correct to two decimal places. There are 12 inches in
a foot; 1 inch is
2.54 centimetres.
Filename: Exercise11_2.java
@author:
Course: BSc Computing
Module: CSY1020 Problem Solving & Programming
Tutor:
@version: 1.0
Date:
*/
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
@SuppressWarnings("serial")
public class Exercise11_2 extends JFrame {
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
28
public void run() {
try {
Exercise11_2 frame = new Exercise11_2();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Exercise11_2() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
contentPane.add(getLblInches());
contentPane.add(getLblFeet());
contentPane.add(getTxtIn());
contentPane.add(getTxtFt());
contentPane.add(getBtnConvert());
contentPane.add(getLblIncm());
}
return result;
29
txtIn = new JTextField();
txtIn.setText("0");
txtIn.setBounds(141, 46, 86, 20);
txtIn.setColumns(10);
}
return txtIn;
}
}
});
btnConvert.setBounds(51, 157, 89, 23);
}
return btnConvert;
}
Implementation: The above program has two text boxes to take input from the user for
feet and inches. The convert button then converts this measurement to centimeters within a
precision of 2 decimal points. This result is then displayed to the user.
Exercise: 15.1: Write a program which inputs two strings from text fields, and which joins
them together. Show the resulting string in a text field.
Code:
/**
30
Exercise: Measurement Conversion. Write a program to input a measurement expressed in
feet
and inches via two text fields. When a button is clicked, convert the measurement to
centimetres
and display it in a text field, correct to two decimal places. There are 12 inches in
a foot; 1 inch is
2.54 centimetres.
Filename: Exercise11_2.java
@author:
Course: BSc Computing
Module: CSY1020 Problem Solving & Programming
Tutor:
@version: 1.0
Date:
*/
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
@SuppressWarnings("serial")
public class Exercise11_2 extends JFrame {
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Exercise11_2 frame = new Exercise11_2();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
31
* Create the frame.
*/
public Exercise11_2() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
contentPane.add(getLblInches());
contentPane.add(getLblFeet());
contentPane.add(getTxtIn());
contentPane.add(getTxtFt());
contentPane.add(getBtnConvert());
contentPane.add(getLblIncm());
}
return result;
32
txtFt.setBounds(141, 92, 86, 20);
txtFt.setColumns(10);
}
return txtFt;
}
}
});
btnConvert.setBounds(51, 157, 89, 23);
}
return btnConvert;
}
Implementation: This program makes simple use of text boxes to get strings from the
user and when the user clicks join, the concatenated version of these strings is displayed.
Exercise: 15.2: Write a program which inputs one string and determines whether or not it is
a palindrome. A palindrome reads the same backwards and forwards, so "abba" is a
palindrome.
Code:
/**
Exercise: Write a program which inputs one string and determines whether or not it is
a palindrome. A palindrome reads the same backwards and forwards, so "abba" is a
palindrome.
Filename: Exercise15_2.java
@author:
Course: BSc Computing
Module: CSY1020 Problem Solving & Programming
Tutor:
@version: 1.0
Date:
*/
import java.awt.EventQueue;
33
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
@SuppressWarnings("serial")
public class Exercise15_2 extends JFrame {
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Exercise15_2 frame = new Exercise15_2();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Exercise15_2() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
contentPane.add(getLblInputString());
contentPane.add(getTxtInp());
contentPane.add(getBtnCheckPalindrome());
contentPane.add(getLblCheckres());
}
34
}
return lblInputString;
}
else
lblCheckres.setText("The string is not a
palindrome.");
}
});
btnCheckPalindrome.setBounds(118, 113, 181, 23);
}
return btnCheckPalindrome;
}
Implementation: This program also makes use of text box to get input from the user and
uses a simple logic to check for palindrome and displays to the user after they click the
check button whether the input string is its palindrome.
35
Conclusion
This assignment was a result of extreme hard work on both coding as well as researching front
on the part of the author. Completing these exercises have certainly been challenging as well as
rewarding and the author thoroughly enjoyed most of the aspects related to this assignment.
Though all the exercises could not be addressed, regretfully due to time constraints, those that
have been addressed have been done in an original manner as possible.
References
Eck, D. J. and Colleges, W. S. (2010) ‘Introduction to Programming Using Java’, Environments.
Huntley, J. et al. (2019) ‘Introduction to Object-Oriented Programming’, in Game Programming
for Artists. doi: 10.1201/b22049-4.
Larsen, A. L. (2001) ‘Java programming’, ACM SIGSOFT Software Engineering Notes. doi:
10.1145/505894.505922.
Loo, A. W.-S. (2007) ‘Java Network Programming’, in Peer-to-Peer Computing. doi: 10.1007/978-
1-84628-747-3_7.
Murrell, P. (2008) ‘A GUI-Builder Approach to grid Graphics’, Study R.
*The forum sites present here have been used for concepts of Java programming; however, it is
less likely to have its influence on direct questions but it does reflect indirect references to
those solved similar problems.
36
37