Вы находитесь на странице: 1из 4

CSC 110 Software Design and Programming I

CSC 110 Software Design and Programming I


Lab Assignment #7
Palindrome (20pnts)
Goals
This lab was designed to reinforce programming concepts from Chapter 4 of Java How to
Program: 8/e. In this lab, you will practice:
• Using selection statements.
• Using sentinel-controlled repetition.
• Using if…else selection statements.
The follow-up question and activity will also give you practice:
• Modifying existing code to perform a similar task.

Requirements
You will need to read and understand Java tutorial pages. You can use the following pages
or your can look for other material in the Web.

Scanner Class: http://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html

print(), println() and printf():


https://docs.oracle.com/javase/tutorial/essential/io/formatting.html

While Statements:
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html

If statements:
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html

General Java 8 documentation: http://docs.oracle.com/javase/8/docs/api/


Note: Use only Netbeans to complete this lab. You may use Snipping Tool or
GreenShot to take the screenshots of the part of the screen.

Introduction: The program template represents a complete working Java program with
one or more key lines of code replaced with comments. Read the problem description and
examine the sample output, then study the template code. Using the problem-solving tips
as a guide, replace the /* */ comments with Java code. Compile and execute the program.
Compare your output with the sample output provided. Then answer the follow-up
questions. The source code for the template is available below the problem.

What is Palindrome? How to find the Palindrome: You must watch this video:

1
CSC 110 Software Design and Programming I

Palindrome or not Java Tutorial: Click on the link below

https://youtu.be/2-TKmwzxtZ8

Problem 1. A palindrome is a sequence of characters that reads the same backward


as forward. For example, each of the following five-digit integers is a
palindrome: 12321, 55555, 45554 and 11611. Write an application that
reads in a five-digit integer and determines whether it is a palindrome. If
the number is not five digits long, display an error message and allow the
user to enter a new value. Use the template code given in Fig 7.1 and
7.2.

Program Template

Fig 7.1 Palindrome. java

1 //Palindrom
publ e.java
ic class Palindrom e
2 //Program tests for a palindrom e
{
3 im port java.ut
//checks ifila.Scanner;
5-digit num ber is a palindrom e public
4 void checkPalindrom e()
5 {
6 Scanner input = new Scanner( System .in );
7
8 int num ber;//user input num ber int
9 digit1;//fi rst digit 2
10 int digit2; // second digit int
11 digit4; // fourth digit int digit5; //
12 fifth digit
13 int digits; // num ber of digits in input
14
16 digits = 0;
17
18 /* W rite code that inputs a fi ve-digit num ber.D isplay an error m essage
19 if the num ber is not fi ve digits.Loop untila valid input is received.*/
CSC 20 110 Software Design and Programming I
21 /* W rite code that separates the digits in the fi ve digit num ber.U se division to isolate
22 the left-m ost digit in the num ber,use a rem ainder calculation to rem ove that digit
23 from the num ber.Then repeat this process.*/
24
25
/* W rite code that determ ines w hether the fi rst and last digits are identical and
26
Fig277.2 PalindromeTest. Java and Fourth digits are identical.O utput w hether or not the original
the second
string is a palindrom e.*/
128 // PalindromeTest.java
229 // Test} application
// end m ethodfor class Palindrome
checkPal indrom e
330 public
} /class
/ end PalindromeTest
class Palindrom e
431 {
532 public static void main( String args[] )
633 {
734 Palindrome application = new Palindrome();
835 application.checkPalindrome();
9 } // end main
10 } // end class PalindromeTest

Problem-Solving Tips for Problem 1


1. Determine the number of digits in the value input by the user and assign the
result to digits. Use a while loop to determine whether the user input contains
the proper number of digits. In the condition, determine whether digits is equal
to five. If not, input a new value from the user and determine whether the new
value contains the proper number of digits. When the number of digits is five,
the loop should terminate.
2. Use division and remainder calculations to obtain the separate digits. For a five-
digit number to be a palindrome, the first and fifth digits must be the same and
the second and fourth digits must be the same.
3. Be sure to follow the spacing and indentation conventions mentioned in the text.
4. If you have any questions as you proceed, ask your lab instructor for assistance.

Sample Output

Enter a 5-digit number: 1234


Number must be 5 digits
Enter a 5-digit number: 123456
Number must be 5 digits Enter a
5-digit number: 54345 54345 is a
palindrome!!!
Enter a 5-digit number: 44567
44567 is not a palindrome!

Problem 2. Modify the program to determine whether a seven-digit number is a


palindrome. Before modifying, rename the files to Palindrome2.java and
Palindrome2Test.java as file names.
Sample output for Problem 2

3
CSC 110 Software Design and Programming I

What to turn in?


 Create a new word document Save the document as “CSC110-Lab7-
yourfistname.docx”
 Read below on what your lab report should include for each problem. Include
everything listed in a your report, save the document
 To Submit, Use Canvas “Lab7-Chp4-Palindrome” assignment link to upload your
complete (CSC110-Lab7-yourfistname.docx) word document.

Problem 1
 Source code of your Problem1: Palindrome and PalindromeTest classes.
 Screen shots of a sample execution.
 A report that explains how your class works and observations you may have from the
execution of PalindromeTest, as well as things that may have been surprising.
 Explain what tests you run in order to make sure that the methods you introduced
worked correctly.
 Explain any challenged encountered and how you overcome the challenges
Problem 2
 Source code of your Palindrome2 and Palindrome2Test classes.
 Screen shots of a sample execution. .
 Explain what tests you run in order to make sure that the methods you introduced
worked correctly.

Вам также может понравиться