Chapter 9 Lab Text Processing and Wrapper Classes

Similar documents
CS 101 Fall 2006 Midterm 1 Name: ID:

Chapter 3 Lab Decision Structures

Task #1 The if Statement, Comparing Strings, and Flags

Chapter 5 Lab Methods

Chapter 5 Lab Methods

Chapter 5 Lab Methods

Example Program. public class ComputeArea {

Check out how to use the random number generator (introduced in section 4.11 of the text) to get a number between 1 and 6 to create the simulation.

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

Faculty of Science COMP-202B - Introduction to Computing I (Winter 2009) Midterm Examination

Advanced Programming - CS239

Object-Oriented Design Lecture 3 CSU 370 Fall 2007 (Pucella) Friday, Sep 14, 2007

COMP-202: Foundations of Programming. Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015

Faculty of Science Midterm. COMP-202B - Introduction to Computing I (Winter 2008)

Chapter 6 Lab Classes and Objects

BoxPrint and Console Input Verifiers

Classes Basic Overview

CS 101 Fall 2006 Midterm 3 Name: ID:

Please answer the following questions. Do not re-code the enclosed codes if you have already completed them.

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Chapter 6 Lab Classes and Objects

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Computer Science is...

5) (4 points) What is the value of the boolean variable equals after the following statement?

Programming with Java

Chapter 1. Introduction to Computers and Programming. M hiwa ahmad aziz

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Midterm Examination

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

Chapter 4 Defining Classes I

McGill University School of Computer Science COMP-202A Introduction to Computing 1

Mr. Monroe s Guide to Mastering Java Syntax

Linked Lists. private int num; // payload for the node private Node next; // pointer to the next node in the list }

H212 Introduction to Software Systems Honors

Fundamentals of Programming Data Types & Methods

Menu Driven Systems. While loops, menus and the switch statement. Mairead Meagher Dr. Siobhán Drohan. Produced by:

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4

CS 101 Spring 2006 Final Exam Name: ID:

Getting started with Java

Oct Decision Structures cont d

Section 2.2 Your First Program in Java: Printing a Line of Text

Program Fundamentals

Computational Expression

ITI1120 Introduction to Computing I Exercise Workbook Fall 2012

Introduction to Computer Science Unit 2. Exercises

Chapter 1 Lab Algorithms, Errors, and Testing

Lecture 8 " INPUT " Instructor: Craig Duckett

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

CS 101 Spring 2007 Midterm 2 Name: ID:

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

CSE 142, Winter 2007 Final Exam. Name:

STUDENT LESSON A12 Iterations

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

Java Simple Data Types

Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4

Computer Components. Software{ User Programs. Operating System. Hardware

Exam 2. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 40 Obtained Marks:

Java Bytecode (binary file)

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Array. Prepared By - Rifat Shahriyar

Over and Over Again GEEN163

1 Short Answer (10 Points Each)

Using Java Classes Fall 2018 Margaret Reid-Miller

COMP String and Console I/O. Yi Hong May 18, 2015

Intro to Programming in Java Practice Midterm

CS 231 Data Structures and Algorithms, Fall 2016

CS 101 Fall 2005 Midterm 2 Name: ID:

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

AP Computer Science A

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Lesson:9 Working with Array and String

The Irving K. Barber School of Arts and Sciences COSC 111 Final Exam Winter Term II Instructor: Dr. Bowen Hui. Tuesday, April 19, 2016

Question: Total Points: Score:

Exercise 4: Loops, Arrays and Files

Midterm Exam 2 CS 455, Fall 2013

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

Question: Total Points: Score:

ME30 Lab3 Decisions. February 20, 2019

CS Week 5. Jim Williams, PhD

assertion: A statement that is either true or false.

There are several files including the start of a unit test and the method stubs in MindNumber.java. Here is a preview of what you will do:

Software and Programming 1

First Java Program - Output to the Screen

COE 212 Engineering Programming. Welcome to Exam I Tuesday November 11, 2014

Darrell Bethea May 10, MTWRF 9:45-11:15 AM Sitterson 011

Introduction to JAVA

Arrays. Eng. Mohammed Abdualal

CSE 142, Spring 2009, Sample Final Exam #2. Good luck!

COMP-202: Foundations of Programming. Lecture 2: Java basics and our first Java program! Jackie Cheung, Winter 2015

Lec 3. Compilers, Debugging, Hello World, and Variables

COMP-202B - Introduction to Computing I (Winter 2011) - All Sections Example Questions for In-Class Quiz

Math Modeling in Java: An S-I Compartment Model

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

CISC-124. This week we continued to look at some aspects of Java and how they relate to building reliable software.

COMP 110 Programming Exercise: Simulation of the Game of Craps

CS 180. Recitation 8 / {30, 31} / 2007

CS 116. Lab Assignment # 1 1

1. Traffic Control CIT/CSE 230 PROGRAMMING PROJECT 4

CSE 142, Autumn 2010 Final Exam Wednesday, December 15, Name:

Transcription:

Lab Objectives Chapter 9 Lab Text Processing and Wrapper Classes Use methods of the Character class and String class to process text Be able to use the StringTokenizer and StringBuffer classes Introduction In this lab we ask the user to enter a time in military time (24 hours). The program will convert and display the equivalent conventional time (12 hour with AM or PM) for each entry if it is a valid military time. An error message will be printed to the console if the entry is not a valid military time. Think about how you would convert any military time 00:00 to 23:59 into conventional time. Also think about what would be valid military times. To be a valid time, the data must have a specific form. First, it should have exactly 5 characters. Next, only digits are allowed in the first two and last two positions, and that a colon is always used in the middle position. Next, we need to ensure that we never have over 23 hours or 59 minutes. This will require us to separate the substrings containing the hours and minutes. When converting from military time to conventional time, we only have to worry about times that have hours greater than 12, and we do not need to do anything with the minutes at all. To convert, we will need to subtract 12, and put it back together with the colon and the minutes, and indicate that it is PM. Keep in mind that 00:00 in military time is 12:00 AM (midnight) and 12:00 in military time is 12:00 PM (noon). We will need to use a variety of Character class and String class methods to validate the data and separate it in order to process it. We will also use a Character class method to allow the user to continue the program if desired. The String Tokenizer class will allow us to process a text file in order to decode a secret message. We will use the first letter of every 5 th token read in from a file to reveal the secret message. Task #1 Character and String Class Methods 48 1. Copy the files Time.java (code listing 9.1) and TimeDemo.java (code listing 9.2) from the StudentCD or as directed by your instructor. 2. In the Time.java file, add conditions to the decision structure which validates the data. Conditions are needed that will a. Check the length of the string b. Check the position of the colon c. Check that all other characters are digits

3. Add lines that will separate the string into two substrings containing hours and minutes. Convert these substrings to integers and save them into the instance variables. 4. In the TimeDemo class, add a condition to the loop that converts the user s answer to a capital letter prior to checking it. 5. Compile, debug, and run. Test out your program using the following valid input: 00:00, 12:00, 04:05, 10:15, 23:59, 00:35, and the following invalid input: 7:56, 15:78, 08:60, 24:00, 3e:33, 1:111. Task #2 StringTokenizer and StringBuffer classes 1. Copy the file secret.txt (code listing 9.3) from the Student CD or as directed by your instructor. This file is only one line long. It contains 2 sentences. 2. Write a main method that will read the file secret.txt, separate it into word tokens. 3. You should process the tokens by taking the first letter of every fifth word, starting with the first word in the file. These letters should converted to capitals, then be appended to a StringBuffer object to form a word which will be printed to the console to display the secret message. Code Listing 9.1 (Time.java) /**Represents time in hours and minutes using the customary conventions*/ public class Time /**hours in conventional time*/ private int hours; /**minutes in conventional time*/ private int minutes; /**true if afternoon time, false if morning time*/ private boolean afternoon; /**Constructs a cutomary time (12 hours, am or pm) from a military time ##:## @param militarytime time in the military format ##:##*/ public Time(String militarytime) //Check to make sure something was entered if (militarytime == null) System.out.println( "You must enter a valid miliary time." ); //Check to make sure there are 5 characters if (//CONDITION TO CHECK LENGTH OF STRING) 49

//Check to make sure the colon is in //the correct spot if (//CONDITION TO CHECK COLON POSITION) //Check to make sure all other characters are digits if (//CONDITION TO CHECK FOR DIGIT) if (//CONDITION TO CHECK FOR DIGIT) if (//CONDITION TO CHECK FOR DIGIT) if (//CONDITION TO CHECK FOR DIGIT) //SEPARATE THE STRING INTO THE HOURS //AND THE MINUTES, CONVERTING THEM TO //INTEGERS AND STORING INTO THE //INSTANCE VARIABLES //validate hours and minutes are valid values if(hours > 23) 50

if(minutes > 59) //convert military time to conventional time //for afternoon times if (hours > 12) hours = hours - 12; afternoon = true; //account for midnight if (hours == 0) hours = 12; //account for noon if (hours == 12) afternoon = true; //morning times don't need converting /**tostring method returns a conventional time @return a conventional time with am or pm*/ public String tostring() String am_pm; String zero = ""; if (afternoon) am_pm = "PM"; am_pm = "AM"; if (minutes < 10) 51

zero = "0"; return hours + ":" + zero + minutes + " " + am_pm; Code Listing 9.2 (TimedDemo.java) public class TimeDemo public static void main (String [ ] args) Scanner keyboard = new Scanner(System.in); char answer = 'Y'; String enteredtime; String response; while (//CHECK ANSWER AFTER CONVERTING TO CAPITAL) System.out.print( "Enter a miitary time using the ##:## form "); enteredtime = keyboard.nextline(); Time now = new Time (enteredtime); System.out.println( "Do you want to enter another (Y/N)? "); response = keyboard.nextline(); answer = response.charat(0); Code Listing 9.3 (secret.txt) January is the first month and December is the last. Violet is a purple color as are lilac and plum. 52