Cosc 1436 java programming/tutorialoutlet

W

FOR MORE CLASSES VISIT tutorialoutletdotcom NameFirstNamePhoneNumberTranslator.java Coin.java LastNameFirstNameCoinDriver.java ModifiedCoin.java Note 1: Unless otherwise mentioned, you are asked to upload ONLY your java source files through blackboard. Email submission is not accepted, because of confusion in grading.

COSC 1436 Java programming
FOR MORE CLASSES VISIT
tutorialoutletdotcom
NameFirstNamePhoneNumberTranslator.java
Coin.java
LastNameFirstNameCoinDriver.java
ModifiedCoin.java Note 1: Unless otherwise mentioned, you are asked
to upload ONLY your java source files through
blackboard. Email submission is not accepted, because of confusion in
grading.
Note 2: If your programs contain any syntactical errors, no points will be
given. Thus, please make sure your
programs are properly compiled with computers at the CS labs, not only
in your laptop or desktop
environments.
Note 3: No late submission will be accepted, thus keep the deadline.
Note 4: Grading will be divided into two categories, formatting and
logic, where formatting compromise
25% of your total grade. Formatting will be based on the following
rules. Rule 1: Naming is an important issue in Java. Not only you should
you define meaningful variable names,
but should also give appropriate names for the physical java file, which
must be the same as the public class
name that you edit.
Unless otherwise mentioned, you will follow the industry standard for
Java naming convention:
(1) Java Classes start in uppercase and each individual word in the class
name is capitalized;
(2) All Java methods and variables start in lowercase and each individual
word in the method and
variable is capitalized;
(3) Each final variable (known as a constant) should be written in all
uppercase.
Rule 2: There should be a space around all operators (e.g., 3 + 5, not
3+5). In addition, spacing with regards
to parentheses should be consistent.
Dr. An, Mrs. Varol & Dr. Rabieh: COSC 1436 Assignment #3 –
Spring 2017 Page 2 of 6
Rule 3: In addition to the Java naming conventions, you are asked to add
your name in front of each class
name like LastNameFirstNameClassName.java.
For instance, if your name is “John Doe” and the class name is
“RightTriangle”, then the class name in your
source code should be “DoeJohnRightTriangle” and your corresponding
physical file name should be
“DoeJohnRightTriangle.java”.
Rule 4: Everything nested inside of an open brace should be indented
with regular-sized spaces (say, 4 or 8
spaces). The open brace for functions and classes should (1) come at the
end of the line and be preceded by a
space like
public class DoeJohnRightTriangle {
public static void main( String args ) {
}
} or (2) start with the new line as shown below:
public class DoeJohnRightTriangle
{
public static void main( String args )
{
}
} Rule 5: Always type block Javadoc comments to include title of the
project, program’s purpose, your name,
the date, and the version number as in the lectures or in the labs. For
example,
/*********************************************************
**********
@Title:
LastNameFirstNameClassName
@Purpose:
To verify the edit, compile, execute function in Textpad
@Author:
(your last & first name)
@Date:
(today’s date)
@Version:
1.0
**********************************************************
**********/ Dr. An, Mrs. Varol & Dr. Rabieh: COSC 1436
Assignment #3 – Spring 2017 Page 3 of 6 Question 1 (50 points):
Reading and Writing to file
Write a program that
(1) reads alphabetic phone numbers from the text file,
“AlphabeticPhoneNumbers.txt”,
(2) translates each of the alphabetic phone numbers to the equivalent
numeric phone number, and
(3) writes the translated numeric numbers into a text file,
“NumericPhoneNumbers.txt”.
(4) uses Javadoc comments to comment every method you write, use
@params and @return tags to write
comments about the parameters and return types of methods
For example, when the program reads 555-GET-FOOD, it should write
555-438-3663 into
NumericPhoneNumbers.txt.
On a standard telephone, the alphabetic letters are mapped to numbers in
the following fashion;
A, B and C = 2
D, E, and F = 3
G, H, and I = 4
J, K, and L = 5
M, N, and O = 6
P, Q, R, and S = 7
T, U, and V = 8
W, X, Y, and Z = 9
You need to break your code into following methods: getFile: It opens
the AlphabeticPhoneNumbers.txt file, and reads each line (a phone
number) as a
String from the file. It calls isValid method for validity check of the
phone number read. If isValid
method returns true, then it calls getPhoneNumber method for
translation. isValid: It returns true if the alphabetical phone number:
(1) includes letters, numbers or dashes, and
(2) its length is 12 , 13 or 14 (with country code).
Otherwise, it returns false. getPhoneNumber: It accepts one argument
that is the alphabetical phone number passed from
getFile mehod, translates it to the equivalent numeric phone number, and
writes the result in
NumericPhoneNumbers.txt file.
Here are the necessary steps:
A. getFile method needs to
1. create object(s) of file input/output related classes to open the text file
(AlphabeticPhoneNumber.txt)
2. create an object of Scanner class to read data from the text file
3. declare any necessary variables that will be used in the code
4. read each line from the text file until no more line is left. (Hint: Recall
hasNext() method). (a loop is needed!)
a. read each line (an alphabetical phone number) as a String
b. call isValid method to check if the alphabetical phone number is
valid or not. If yes, call getPhoneNumber method by passing the
phone number, otherwise read the next line.
* Don’t forget to handle exceptions. Recall throws IOException
B. getPhoneNumber method needs to Dr. An, Mrs. Varol & Dr.
Rabieh: COSC 1436 Assignment #3 – Spring 2017 Page 4 of 6
1. accept a String data (an alphabetical phone number) as its argument
2. have a loop to access each character of the String and do the
followings
until we reach the end of the String:
a. access each character in that String by specifying index number
-> charAt(i)
b. check whether the character is a digit, letter or a special
character.
* Hint: import java.lang.Character =>
In order to test if a character is a letter or digit, you
should use isLetterOrDigit() method that is provided in
Character wrapper class, also Character wrapper class has other
methods such as isLetter, isDigit, etc. or remember Unicode that
provides a unique number for every character.
c. If it is a letter, convert it into the corresponding digit (see
the map for it above).
* Don’t forget to handle exceptions. Recall throws IOException.
3. create necessary objects of file input/output related classes to open the
text file, and to write the translated numerical phone number.
C. isValid method needs to
1. check each alphabetical phone number for validity, and if it is valid,
return true, otherwise return false. the alphabetical phone number is
valid if it includes:
a. letters, numbers or dashes, and
b. its length (the number of characters in the phone number read) is
12 or 14 (with country code)
D. Close the text files. Hint: Your program should display the result as
shown in below
1-800-FLOWERS = 1-800-3569377
1-800-GOT-JUNK = 1-800-468-5865
555-GET-FOOD = 555-438-3663
1-800-PET-MEDS = 1-800-738-6337
1-800-LAWYERS = 1-800-5299377
1-800-GO-FONTS = 1-800-46-36687
713-333-MOVE = 713-333-6683
Grading criteria include documentation, descriptive variable names, and
adherence to the coding convention
noted on pages 1 & 2.
Your file will have the following documentation header:
Your file will have the header and the class definition as follows:
/*********************************************************
**********
@Title:
LastNamePhoneNumberTranslator
@Purpose:
To get familiar with input/output classes, String object, and various
operations in Java
@Author:
(your last first name)
@Date:
(today’s date)
@Version:
1.0
**********************************************************
***********/ Dr. An, Mrs. Varol & Dr. Rabieh: COSC 1436
Assignment #3 – Spring 2017 Page 5 of 6 Question 2 (30 points): Coin
Toss Class
Write a class named Coin. The Coin class should have the following
field and methods:
Coin
- sideUp : String
+ Coin()
+ toss(): void
+ getSideUp(): String The sideUP field will hold either “heads” or
“tails” indicating the side of the coin that is facing up.
The constructor randomly determines the side of the coin that is facing
up (“heads” or “tails”) and
initialized the sideUP field accordingly.
The toss method simulates the tossing of the coin. When the toss method
is called, it randomly
determines the side of the coin that is facing up and sets the sideUP field
accordingly.
The getSideUp method returns the value of the sideUp field. Write a
program (driver class) that demonstrates the Coin class. The program
should create an instance of
the class and display the side that is initially facing up. Then, use a loop
to toss the coin 40 times. Each time
the coin is tossed, display the side that is facing up. The program should
keep count of the number of time
heads is facing up and the number of times tails is facing up, and display
those values after the loop finishes. Your file will have the following
documentation comments before the class header:
/**
@Title:
@Purpose:
@Author:
@Date:
@Version:
*/ Coin
To practice random class and objects.
(your last first name)
(today’s date)
1.0 /**
@Title:
@Purpose:
@Author:
@Date:
@Version:
*/ LastNameFirstNameCoinDriver
To practice random class and objects.
(your last first name)
(today’s date)
1.0 Question 3 (20 points): Coin Toss Class with multiple constructors
and methods
overloading
Write a program that demonstrate a modified version of the Coin class.
Your class should have the name
ModifiedCoin. The ModifiedCoin class should have two constructors
with zero arguments; the default
constructor and a constructor that takes the sideUp as an argument. The
ModifiedCoin should have two
versions of toss method. The first version takes zero arguments and sets
the sideUP field randomly (a fair
Dr. An, Mrs. Varol & Dr. Rabieh: COSC 1436 Assignment #3 –
Spring 2017 Page 6 of 6
toss). The second version of the toss method takes a String argument that
can be either head or tail and
returns the same String (unfair toss).
Write a program that create two instances (objects) of the ModifiedCoin
class using the two different
constructors and displays the side that is initially facing up. Call the two
versions of the toss method and
display the side that is facing up.
Your file will have the following documentation comments before the
class header:
/**
@Title:
@Purpose:
@Author:
@Date:
@Version:
*/ ModifiedCoin
To practice Method overloading and class with multiple constructors.
(your last first name)
(today’s date)
/**
@Title: Coin
@Purpose: To practice random class and objects.
@Author: (Martinez, James)
@Date: (03/20/2017)
@Version: 1.0
*/
import java.util.*;
public class Coin
{
private String sideUp;
//*will call my toss method*/
public Coin()
{
toss();
}
/*this method will determine whether or not the coin is heads or tails*/
public void toss()
{
Random myRand = new Random();
int face = myRand.nextInt(2);
if(face == 0)
{
sideUp = "heads";
}
else
{
sideUp = "tails";
}
}
/*this method should return the number of heads and tails that result
from my
coin toss*/
public String getSideUp()
{
return sideUp;
}
}
/**
@Title: MartinezJamesCoinDriver
@Purpose: To practice random class and objects.
@Author: (Martinez, James)
@Date: (03/20/2017)
@Version: 1.0
*/
public class CoinDriver
{
public static void main(String args)
{
Coin coin = new Coin();
int headsSum = 0;
int tailsSum = 0;
/*This for loop will simulate the coin being tossed*/
for(int i = 1; i <= 40; i++)
{
coin.toss();
System.out.println(coin.getSideUp());
if(coin.getSideUp().equals("heads"))
{
headsSum++;
}
else if(coin.getSideUp().equals("tails"))
{
tailsSum++;
} }
System.out.println("Total number of heads: " + headsSum +
"nTotal number of
tails: " + tailsSum);
}
}
***************************************************

Recomendados

T1 von
T1T1
T1lksoo
479 views10 Folien
Compiler design-lab-manual v-cse von
Compiler design-lab-manual v-cseCompiler design-lab-manual v-cse
Compiler design-lab-manual v-cseravisharma159932
567 views59 Folien
Handout#01 von
Handout#01Handout#01
Handout#01Sunita Milind Dol
135 views8 Folien
Introduction of C++ By Pawan Thakur von
Introduction of C++ By Pawan ThakurIntroduction of C++ By Pawan Thakur
Introduction of C++ By Pawan ThakurGovt. P.G. College Dharamshala
1.4K views22 Folien
Handout#05 von
Handout#05Handout#05
Handout#05Sunita Milind Dol
155 views7 Folien
9781439035665 ppt ch03 von
9781439035665 ppt ch039781439035665 ppt ch03
9781439035665 ppt ch03Terry Yoast
350 views59 Folien

Más contenido relacionado

Was ist angesagt?

Handout#04 von
Handout#04Handout#04
Handout#04Sunita Milind Dol
148 views5 Folien
Let's us c language (sabeel Bugti) von
Let's us c language (sabeel Bugti)Let's us c language (sabeel Bugti)
Let's us c language (sabeel Bugti)Unviersity of balochistan quetta
54 views57 Folien
Assignment11 von
Assignment11Assignment11
Assignment11Sunita Milind Dol
89 views6 Folien
Structures-2 von
Structures-2Structures-2
Structures-2arshpreetkaur07
140 views30 Folien
Assignment1 von
Assignment1Assignment1
Assignment1Sunita Milind Dol
81 views9 Folien
Handout#03 von
Handout#03Handout#03
Handout#03Sunita Milind Dol
146 views10 Folien

Was ist angesagt?(20)

Functions, Strings ,Storage classes in C von arshpreetkaur07
 Functions, Strings ,Storage classes in C Functions, Strings ,Storage classes in C
Functions, Strings ,Storage classes in C
arshpreetkaur0757 views
Top C Language Interview Questions and Answer von Vineet Kumar Saini
Top C Language Interview Questions and AnswerTop C Language Interview Questions and Answer
Top C Language Interview Questions and Answer
Vineet Kumar Saini29.7K views
Python Interview questions 2020 von VigneshVijay21
Python Interview questions 2020Python Interview questions 2020
Python Interview questions 2020
VigneshVijay21246 views
Comp 122 lab 7 lab report and source code von pradesigali1
Comp 122 lab 7 lab report and source codeComp 122 lab 7 lab report and source code
Comp 122 lab 7 lab report and source code
pradesigali1166 views
Coursebreakup von PCTE
CoursebreakupCoursebreakup
Coursebreakup
PCTE281 views

Similar a Cosc 1436 java programming/tutorialoutlet

Android coding standard von
Android coding standard Android coding standard
Android coding standard Rakesh Jha
3.1K views10 Folien
CSE 1310 – Spring 21Introduction to ProgrammingLab 4 Arrays and Func von
CSE 1310 – Spring 21Introduction to ProgrammingLab 4 Arrays and FuncCSE 1310 – Spring 21Introduction to ProgrammingLab 4 Arrays and Func
CSE 1310 – Spring 21Introduction to ProgrammingLab 4 Arrays and FuncMargenePurnell14
2 views29 Folien
C Language (All Concept) von
C Language (All Concept)C Language (All Concept)
C Language (All Concept)sachindane
2.3K views60 Folien
The Lab assignment will be graded out of 100 points.  There are .docx von
The Lab assignment will be graded out of 100 points.  There are .docxThe Lab assignment will be graded out of 100 points.  There are .docx
The Lab assignment will be graded out of 100 points.  There are .docxjmindy
4 views22 Folien
Java execise von
Java execiseJava execise
Java execiseKeneth miles
2.1K views102 Folien
OCA Java SE 8 Exam Chapter 1 Java Building Blocks von
OCA Java SE 8 Exam Chapter 1 Java Building BlocksOCA Java SE 8 Exam Chapter 1 Java Building Blocks
OCA Java SE 8 Exam Chapter 1 Java Building Blocksİbrahim Kürce
4.6K views50 Folien

Similar a Cosc 1436 java programming/tutorialoutlet(20)

Android coding standard von Rakesh Jha
Android coding standard Android coding standard
Android coding standard
Rakesh Jha3.1K views
CSE 1310 – Spring 21Introduction to ProgrammingLab 4 Arrays and Func von MargenePurnell14
CSE 1310 – Spring 21Introduction to ProgrammingLab 4 Arrays and FuncCSE 1310 – Spring 21Introduction to ProgrammingLab 4 Arrays and Func
CSE 1310 – Spring 21Introduction to ProgrammingLab 4 Arrays and Func
C Language (All Concept) von sachindane
C Language (All Concept)C Language (All Concept)
C Language (All Concept)
sachindane2.3K views
The Lab assignment will be graded out of 100 points.  There are .docx von jmindy
The Lab assignment will be graded out of 100 points.  There are .docxThe Lab assignment will be graded out of 100 points.  There are .docx
The Lab assignment will be graded out of 100 points.  There are .docx
jmindy4 views
OCA Java SE 8 Exam Chapter 1 Java Building Blocks von İbrahim Kürce
OCA Java SE 8 Exam Chapter 1 Java Building BlocksOCA Java SE 8 Exam Chapter 1 Java Building Blocks
OCA Java SE 8 Exam Chapter 1 Java Building Blocks
İbrahim Kürce4.6K views
Cmps 260, fall 2021 programming assignment #3 (125 points) von mehek4
Cmps 260, fall 2021 programming assignment #3 (125 points)Cmps 260, fall 2021 programming assignment #3 (125 points)
Cmps 260, fall 2021 programming assignment #3 (125 points)
mehek4118 views
Mca1020 programming in c von smumbahelp
Mca1020  programming in cMca1020  programming in c
Mca1020 programming in c
smumbahelp106 views
Essay On Cmis 102 Assignment 1 von Laura Martin
Essay On Cmis 102 Assignment 1Essay On Cmis 102 Assignment 1
Essay On Cmis 102 Assignment 1
Laura Martin2 views
Answer the following questions.1.What is the screen output o.docx von emelyvalg9
Answer the following questions.1.What is the screen output o.docxAnswer the following questions.1.What is the screen output o.docx
Answer the following questions.1.What is the screen output o.docx
emelyvalg93 views
Cis 1403 lab1- the process of programming von Hamad Odhabi
Cis 1403 lab1- the process of programmingCis 1403 lab1- the process of programming
Cis 1403 lab1- the process of programming
Hamad Odhabi112 views
Lewis jssap3 e_labman02 von auswhit
Lewis jssap3 e_labman02Lewis jssap3 e_labman02
Lewis jssap3 e_labman02
auswhit1.8K views
Devry cis 170 c i lab 5 of 7 arrays and strings von jody zoll
Devry cis 170 c i lab 5 of 7 arrays and stringsDevry cis 170 c i lab 5 of 7 arrays and strings
Devry cis 170 c i lab 5 of 7 arrays and strings
jody zoll46 views
Bt0065, c programming and data structures von smumbahelp
Bt0065, c programming and data structuresBt0065, c programming and data structures
Bt0065, c programming and data structures
smumbahelp143 views

Último

unidad 3.pdf von
unidad 3.pdfunidad 3.pdf
unidad 3.pdfMarcosRodriguezUcedo
106 views38 Folien
Structure and Functions of Cell.pdf von
Structure and Functions of Cell.pdfStructure and Functions of Cell.pdf
Structure and Functions of Cell.pdfNithya Murugan
701 views10 Folien
Women from Hackney’s History: Stoke Newington by Sue Doe von
Women from Hackney’s History: Stoke Newington by Sue DoeWomen from Hackney’s History: Stoke Newington by Sue Doe
Women from Hackney’s History: Stoke Newington by Sue DoeHistory of Stoke Newington
157 views21 Folien
MercerJesse2.1Doc.pdf von
MercerJesse2.1Doc.pdfMercerJesse2.1Doc.pdf
MercerJesse2.1Doc.pdfjessemercerail
237 views5 Folien
Psychology KS4 von
Psychology KS4Psychology KS4
Psychology KS4WestHatch
90 views4 Folien
Create a Structure in VBNet.pptx von
Create a Structure in VBNet.pptxCreate a Structure in VBNet.pptx
Create a Structure in VBNet.pptxBreach_P
75 views8 Folien

Último(20)

Structure and Functions of Cell.pdf von Nithya Murugan
Structure and Functions of Cell.pdfStructure and Functions of Cell.pdf
Structure and Functions of Cell.pdf
Nithya Murugan701 views
Psychology KS4 von WestHatch
Psychology KS4Psychology KS4
Psychology KS4
WestHatch90 views
Create a Structure in VBNet.pptx von Breach_P
Create a Structure in VBNet.pptxCreate a Structure in VBNet.pptx
Create a Structure in VBNet.pptx
Breach_P75 views
Solar System and Galaxies.pptx von DrHafizKosar
Solar System and Galaxies.pptxSolar System and Galaxies.pptx
Solar System and Galaxies.pptx
DrHafizKosar94 views
Ch. 7 Political Participation and Elections.pptx von Rommel Regala
Ch. 7 Political Participation and Elections.pptxCh. 7 Political Participation and Elections.pptx
Ch. 7 Political Participation and Elections.pptx
Rommel Regala105 views
Narration lesson plan von TARIQ KHAN
Narration lesson planNarration lesson plan
Narration lesson plan
TARIQ KHAN59 views
AI Tools for Business and Startups von Svetlin Nakov
AI Tools for Business and StartupsAI Tools for Business and Startups
AI Tools for Business and Startups
Svetlin Nakov111 views
ISO/IEC 27001 and ISO/IEC 27005: Managing AI Risks Effectively von PECB
ISO/IEC 27001 and ISO/IEC 27005: Managing AI Risks EffectivelyISO/IEC 27001 and ISO/IEC 27005: Managing AI Risks Effectively
ISO/IEC 27001 and ISO/IEC 27005: Managing AI Risks Effectively
PECB 598 views
Classification of crude drugs.pptx von GayatriPatra14
Classification of crude drugs.pptxClassification of crude drugs.pptx
Classification of crude drugs.pptx
GayatriPatra1492 views
Monthly Information Session for MV Asterix (November) von Esquimalt MFRC
Monthly Information Session for MV Asterix (November)Monthly Information Session for MV Asterix (November)
Monthly Information Session for MV Asterix (November)
Esquimalt MFRC58 views
7 NOVEL DRUG DELIVERY SYSTEM.pptx von Sachin Nitave
7 NOVEL DRUG DELIVERY SYSTEM.pptx7 NOVEL DRUG DELIVERY SYSTEM.pptx
7 NOVEL DRUG DELIVERY SYSTEM.pptx
Sachin Nitave61 views
Pharmaceutical Inorganic Chemistry Unit IVMiscellaneous compounds Expectorant... von Ms. Pooja Bhandare
Pharmaceutical Inorganic Chemistry Unit IVMiscellaneous compounds Expectorant...Pharmaceutical Inorganic Chemistry Unit IVMiscellaneous compounds Expectorant...
Pharmaceutical Inorganic Chemistry Unit IVMiscellaneous compounds Expectorant...
Ms. Pooja Bhandare109 views

Cosc 1436 java programming/tutorialoutlet

  • 1. COSC 1436 Java programming FOR MORE CLASSES VISIT tutorialoutletdotcom NameFirstNamePhoneNumberTranslator.java Coin.java LastNameFirstNameCoinDriver.java ModifiedCoin.java Note 1: Unless otherwise mentioned, you are asked to upload ONLY your java source files through blackboard. Email submission is not accepted, because of confusion in grading. Note 2: If your programs contain any syntactical errors, no points will be given. Thus, please make sure your programs are properly compiled with computers at the CS labs, not only in your laptop or desktop environments. Note 3: No late submission will be accepted, thus keep the deadline. Note 4: Grading will be divided into two categories, formatting and logic, where formatting compromise 25% of your total grade. Formatting will be based on the following rules. Rule 1: Naming is an important issue in Java. Not only you should you define meaningful variable names, but should also give appropriate names for the physical java file, which must be the same as the public class name that you edit. Unless otherwise mentioned, you will follow the industry standard for Java naming convention: (1) Java Classes start in uppercase and each individual word in the class name is capitalized; (2) All Java methods and variables start in lowercase and each individual
  • 2. word in the method and variable is capitalized; (3) Each final variable (known as a constant) should be written in all uppercase. Rule 2: There should be a space around all operators (e.g., 3 + 5, not 3+5). In addition, spacing with regards to parentheses should be consistent. Dr. An, Mrs. Varol & Dr. Rabieh: COSC 1436 Assignment #3 – Spring 2017 Page 2 of 6 Rule 3: In addition to the Java naming conventions, you are asked to add your name in front of each class name like LastNameFirstNameClassName.java. For instance, if your name is “John Doe” and the class name is “RightTriangle”, then the class name in your source code should be “DoeJohnRightTriangle” and your corresponding physical file name should be “DoeJohnRightTriangle.java”. Rule 4: Everything nested inside of an open brace should be indented with regular-sized spaces (say, 4 or 8 spaces). The open brace for functions and classes should (1) come at the end of the line and be preceded by a space like public class DoeJohnRightTriangle { public static void main( String args ) { } } or (2) start with the new line as shown below: public class DoeJohnRightTriangle { public static void main( String args ) { } } Rule 5: Always type block Javadoc comments to include title of the project, program’s purpose, your name, the date, and the version number as in the lectures or in the labs. For example,
  • 3. /********************************************************* ********** @Title: LastNameFirstNameClassName @Purpose: To verify the edit, compile, execute function in Textpad @Author: (your last & first name) @Date: (today’s date) @Version: 1.0 ********************************************************** **********/ Dr. An, Mrs. Varol & Dr. Rabieh: COSC 1436 Assignment #3 – Spring 2017 Page 3 of 6 Question 1 (50 points): Reading and Writing to file Write a program that (1) reads alphabetic phone numbers from the text file, “AlphabeticPhoneNumbers.txt”, (2) translates each of the alphabetic phone numbers to the equivalent numeric phone number, and (3) writes the translated numeric numbers into a text file, “NumericPhoneNumbers.txt”. (4) uses Javadoc comments to comment every method you write, use @params and @return tags to write comments about the parameters and return types of methods For example, when the program reads 555-GET-FOOD, it should write 555-438-3663 into NumericPhoneNumbers.txt. On a standard telephone, the alphabetic letters are mapped to numbers in the following fashion; A, B and C = 2 D, E, and F = 3 G, H, and I = 4 J, K, and L = 5
  • 4. M, N, and O = 6 P, Q, R, and S = 7 T, U, and V = 8 W, X, Y, and Z = 9 You need to break your code into following methods: getFile: It opens the AlphabeticPhoneNumbers.txt file, and reads each line (a phone number) as a String from the file. It calls isValid method for validity check of the phone number read. If isValid method returns true, then it calls getPhoneNumber method for translation. isValid: It returns true if the alphabetical phone number: (1) includes letters, numbers or dashes, and (2) its length is 12 , 13 or 14 (with country code). Otherwise, it returns false. getPhoneNumber: It accepts one argument that is the alphabetical phone number passed from getFile mehod, translates it to the equivalent numeric phone number, and writes the result in NumericPhoneNumbers.txt file. Here are the necessary steps: A. getFile method needs to 1. create object(s) of file input/output related classes to open the text file (AlphabeticPhoneNumber.txt) 2. create an object of Scanner class to read data from the text file 3. declare any necessary variables that will be used in the code 4. read each line from the text file until no more line is left. (Hint: Recall hasNext() method). (a loop is needed!) a. read each line (an alphabetical phone number) as a String b. call isValid method to check if the alphabetical phone number is valid or not. If yes, call getPhoneNumber method by passing the phone number, otherwise read the next line. * Don’t forget to handle exceptions. Recall throws IOException B. getPhoneNumber method needs to Dr. An, Mrs. Varol & Dr. Rabieh: COSC 1436 Assignment #3 – Spring 2017 Page 4 of 6 1. accept a String data (an alphabetical phone number) as its argument 2. have a loop to access each character of the String and do the
  • 5. followings until we reach the end of the String: a. access each character in that String by specifying index number -> charAt(i) b. check whether the character is a digit, letter or a special character. * Hint: import java.lang.Character => In order to test if a character is a letter or digit, you should use isLetterOrDigit() method that is provided in Character wrapper class, also Character wrapper class has other methods such as isLetter, isDigit, etc. or remember Unicode that provides a unique number for every character. c. If it is a letter, convert it into the corresponding digit (see the map for it above). * Don’t forget to handle exceptions. Recall throws IOException. 3. create necessary objects of file input/output related classes to open the text file, and to write the translated numerical phone number. C. isValid method needs to 1. check each alphabetical phone number for validity, and if it is valid, return true, otherwise return false. the alphabetical phone number is valid if it includes: a. letters, numbers or dashes, and b. its length (the number of characters in the phone number read) is 12 or 14 (with country code) D. Close the text files. Hint: Your program should display the result as shown in below 1-800-FLOWERS = 1-800-3569377 1-800-GOT-JUNK = 1-800-468-5865 555-GET-FOOD = 555-438-3663 1-800-PET-MEDS = 1-800-738-6337 1-800-LAWYERS = 1-800-5299377 1-800-GO-FONTS = 1-800-46-36687 713-333-MOVE = 713-333-6683 Grading criteria include documentation, descriptive variable names, and adherence to the coding convention
  • 6. noted on pages 1 & 2. Your file will have the following documentation header: Your file will have the header and the class definition as follows: /********************************************************* ********** @Title: LastNamePhoneNumberTranslator @Purpose: To get familiar with input/output classes, String object, and various operations in Java @Author: (your last first name) @Date: (today’s date) @Version: 1.0 ********************************************************** ***********/ Dr. An, Mrs. Varol & Dr. Rabieh: COSC 1436 Assignment #3 – Spring 2017 Page 5 of 6 Question 2 (30 points): Coin Toss Class Write a class named Coin. The Coin class should have the following field and methods: Coin - sideUp : String + Coin() + toss(): void + getSideUp(): String The sideUP field will hold either “heads” or “tails” indicating the side of the coin that is facing up. The constructor randomly determines the side of the coin that is facing up (“heads” or “tails”) and initialized the sideUP field accordingly. The toss method simulates the tossing of the coin. When the toss method is called, it randomly determines the side of the coin that is facing up and sets the sideUP field accordingly.
  • 7. The getSideUp method returns the value of the sideUp field. Write a program (driver class) that demonstrates the Coin class. The program should create an instance of the class and display the side that is initially facing up. Then, use a loop to toss the coin 40 times. Each time the coin is tossed, display the side that is facing up. The program should keep count of the number of time heads is facing up and the number of times tails is facing up, and display those values after the loop finishes. Your file will have the following documentation comments before the class header: /** @Title: @Purpose: @Author: @Date: @Version: */ Coin To practice random class and objects. (your last first name) (today’s date) 1.0 /** @Title: @Purpose: @Author: @Date: @Version: */ LastNameFirstNameCoinDriver To practice random class and objects. (your last first name) (today’s date) 1.0 Question 3 (20 points): Coin Toss Class with multiple constructors and methods overloading Write a program that demonstrate a modified version of the Coin class. Your class should have the name
  • 8. ModifiedCoin. The ModifiedCoin class should have two constructors with zero arguments; the default constructor and a constructor that takes the sideUp as an argument. The ModifiedCoin should have two versions of toss method. The first version takes zero arguments and sets the sideUP field randomly (a fair Dr. An, Mrs. Varol & Dr. Rabieh: COSC 1436 Assignment #3 – Spring 2017 Page 6 of 6 toss). The second version of the toss method takes a String argument that can be either head or tail and returns the same String (unfair toss). Write a program that create two instances (objects) of the ModifiedCoin class using the two different constructors and displays the side that is initially facing up. Call the two versions of the toss method and display the side that is facing up. Your file will have the following documentation comments before the class header: /** @Title: @Purpose: @Author: @Date: @Version: */ ModifiedCoin To practice Method overloading and class with multiple constructors. (your last first name) (today’s date) /** @Title: Coin @Purpose: To practice random class and objects. @Author: (Martinez, James) @Date: (03/20/2017) @Version: 1.0
  • 9. */ import java.util.*; public class Coin { private String sideUp; //*will call my toss method*/ public Coin() { toss(); } /*this method will determine whether or not the coin is heads or tails*/ public void toss() { Random myRand = new Random(); int face = myRand.nextInt(2); if(face == 0) { sideUp = "heads"; } else { sideUp = "tails"; } } /*this method should return the number of heads and tails that result from my coin toss*/ public String getSideUp() { return sideUp; } } /** @Title: MartinezJamesCoinDriver
  • 10. @Purpose: To practice random class and objects. @Author: (Martinez, James) @Date: (03/20/2017) @Version: 1.0 */ public class CoinDriver { public static void main(String args) { Coin coin = new Coin(); int headsSum = 0; int tailsSum = 0; /*This for loop will simulate the coin being tossed*/ for(int i = 1; i <= 40; i++) { coin.toss(); System.out.println(coin.getSideUp()); if(coin.getSideUp().equals("heads")) { headsSum++; } else if(coin.getSideUp().equals("tails")) { tailsSum++; } } System.out.println("Total number of heads: " + headsSum + "nTotal number of tails: " + tailsSum); } } ***************************************************