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.
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);
}
}
***************************************************