COMP 1010 Winter 2015 Assignment 3 online essay editor

 

COMP 1010 Winter 2015 Assignment 3

 

Due Date: Friday, 13 March 2015, before midnight

 

 

Material Covered

  • Loops (while- and do-while loops, for loops)
  • More String manipulation
  • File scanning (using Scanner)
  • Graphics (using StdDraw)

 

 

 

Notes and Instructions – Please Follow Exactly

 

  • Name your Java files as follows: <LastName><FirstName>A3Qn.java (use Q1, Q2 and Q3 for Questions 1, 2 and 3). For example, SmithJohnA3Q1.java is a valid name. Your classes need to be named to match.
  • Follow the programming standards posted on the course website to avoid losing marks.
  • You must  submit  the “Blanket  Honesty  Declaration”,  also  on  the course  website,  before any assignment in COMP 1010 will be marked. This must be submitted to your instructor on paper. Submissions via the D2L Dropbox are not accepted.
  • To submit the assignment you will upload the required files, as specified for each question, to the

D2L Dropbox for Assignment 3 on the course website.

  • For example, hand in SmithJohnA3Q1.java for Question 1 (if your name happens to be John

Smith).

  • You will also put the output of your test runs from DrJava into .txt files, and upload them to the

D2L Dropbox in the same way.

  • Name your output files <LastName><FirstName>A3Qn-output.txt.
  • Do NOT manipulate your output in any way.
  • Complete instructions for handing in your assignment, including an easy way to create the output files, can be found in the “Handin instructions” document in the “General Information” folder on the website.
  • To be eligible to earn full marks, your Java programs must compile and run upon download, without requiring any modifications.

 

 

 

Question 1 — Computer Aided Instruction (CAI)

 

Computers play an important role in education. In this question, you will write a program that will help an elementary school student learn basic arithmetic operations.  Write your program as follows:

 

  1. 1. The char getOperator() method. Write  a method  called  getOperator() that  will prompt  the  student  (using  Sysout.print)  to  enter  one  of  the  three  arithmetic operators ‘+’, ‘-’ and ‘*’, and will return the operator as a char (get the operator using Scanner). Your method must handle invalid inputs; that is, if the input is not one of the three arithmetic  operators  mentioned  above,  then the method  must keep prompting  the student for a valid operator until one is entered.

Hint. Use a do-while to handle the validity of the input.

 

  1. 2. The boolean askAQuestion(char operator) method. Write a method  called askAQuestion() that  will  accept  one  of  the  three  arithmetic  operators  as  a  char parameter. Write this method as follows:

 

  • First, generate two random integer values between 1 and 99 (both inclusive) upon which the operator will be applied.
  • Next, prompt the student to answer that question in a message. You must give the student 3 tries to answer the question correctly. For each incorrect answer, print a message indicating that the answer is not correct and ask them to try again.
  • The method should return true if the student found the correct answer in at most

3 tries, and false otherwise.

 

Note. Think of appropriate  constant  values to use in this method,  and declare  them as global named constants in your program.

 

Hint. Use Math.random() to generate random integer values.

 

  1. 3. The main() method. In the main() method,  you will ask the student  at least once to enter an operator  by calling  the getOperator() method  and then will ask him/her  a question on that operator  by calling the askAQuestion() method.  Then, you will ask the student  whether  s/he would  like to continue  with another  question.  If so, you will prompt  him/her  to enter another  operator  and will ask him/her  a new question  on that operator by calling the getOperator() and askAQuestion() methods,  respectively. Your  program  must  continue  until  the  student  enters  either  ‘n’  or  ‘N’  when  asked whether s/he wants to continue. See the sample outputs below.

 

Here are two sample outputs of the program:

 

“Welcome to Our Computer Aided Instruction!” Enter a valid operator (+ or – or *): /

 

Enter a valid operator (+ or – or *): +

3+58? 62

Awe, nooo!! Try again:

3+58? 61

Great job!

Would you like to continue (y/n)? N

 

End of the program.

The program was written by Stu Dent.

 

 

“Welcome to Our Computer Aided Instruction!” Enter a valid operator (+ or – or *): *

38*14? 245

Awe, nooo!! Try again:

38*14? 357

Awe, nooo!! Try again:

38*14? 425

Sorry, you missed all of your 3 chances!

Would you like to continue (y/n)? Y Enter a valid operator (+ or – or *): –

41-18? 22

Awe, nooo!! Try again:

41-18? 21

Awe, nooo!! Try again:

41-18? 23

Great job!

Would you like to continue (y/n)? n

 

End of the program.

The program was written by Stu Dent.

 

 

 

Hand in: Your Java source code, according to the instructions on page 1. Also, submit one output file containing the results from running your program three times as follows.

 

  • In the first test, the student first enters ‘s’ as an invalid operator, then enters ‘+’, answers the question correctly in the second try and then exits.

 

  • In the second test, the student enters ‘*’ as a valid operator, is not able to answer the question correctly within 3 tries and exits.

 

  • In the third test, the student starts with ‘-’ as the operator and does not answer the question correctly within 3 tries. Then, s/he will continue with ‘*’, answers the question correctly in the first try and then exits.

 

Question 2 — File Auto Correction

 

In this question, you will write a program that reads in a text file specified by the user. It will also allow the user to specify one particular word. It will then “auto-correct”  that word in the file by looking for very similar words. Download the file AutoCorrectMe.txt from the assignment  folder  in D2L,  and  put  it in the  same  location  as  your  Java  program  for  this question. Write your progra m as follows:

 

  1. 1. The String getFileName() method. Write a method called getFileName() that will prompt the user (using  out.print)  to enter the name of a file, and return that name as a String. Your program must keep prompting  the user for a file name until a name is entered that ends with “.txt”.

 

  1. 2. The boolean similarWords(String word1, String word2) method.  This method should return true if 1) the two words have exactly the same length, and 2) they  differ  in  exactly  1  character.   So  similarWords(“text”,”test”) should return  true,  but  similarWords(“tests”,”test”) should  return  false since they’re  not  the  same  length,  and  similarWords(“text”,”nest”) should  return false since more than one letter is different.  It should  do this in a case-insensitive way (make both words all upper case, or all lower case, before comparing them).

 

  1. 3. The String correctThisLine(String  currentLine,  String correctWord) method.

This method should use a Scanner to process all of the words in currentLine. You may assume that the line consists only of words separated by blanks. It should replace every  word  in  the  line  that  is  “similar”  to  correctWord (as  defined  above)  by correctWord itself,  and  leave  every  other  word  unchanged.  The  method  should return the modified line.

 

  1. 4. The main() method. This method should use the getFileName() method to allow the user  to  select  a file.  It should  also  prompt  the  user  for  the  word  that  must  be corrected.  It should then use a Scanner to read all of the lines from this file (use the Scanner’s  nextLine() and hasNextLine() methods),  process  each of the lines using correctThisLine, and print out the results.

 

The supplied file AutoCorrectMe.txt contains:

I like progrimming in Java because programminh allows me to do new things and krogramming is a useful

problem solving skill so lets do a lot of programmings Here are many variations of the word PROgrammin PROGRAMMING Programming programping programmING

 

Here is sample output from the program. The user input and the replaced words are shown in purple:

Enter the name of the file: AutoCorrectMe

The file name must end with .txt

Enter the name of the file: AutoCorrectMe.txt

Which word do you wish to auto correct: Programming

 

The corrected file is:

I like Programming in Java because Programming allows

me to do new things and Programming is a useful

problem solving skill so lets do a lot of programmings Here are many variations of the word PROgrammin PROGRAMMING Programming Programming programmING

 

End of the program.

The program was written by Stu Dent.

 

 

Hand in: Your Java source code, according to the instructions on page 1. Also, submit one output file containing the results from running your program on the supplied AutoCorrectMe.txt file using the word “Programming”, as shown above.

 

 

Question 3 —Drawing a Column Chart for Students’ Grades

 

A chart is a visual representation  of data in which the data is shown by some symbol.  In a column chart the data is shown with vertical rectangular bars with heights proportional  to the values that they represent  (see Figure  2 on the next page for an example).  In this question, you  will  draw  a column  chart  using  StdDraw for the grades  of students  in a class.  Your program will first ask the user for the number of students, followed by their grades, and then will draw a column chart with the grade of each student  shown  as one column.  Write your program as follows:

 

  1. 1. The void  drawAxes() method.  Write a method called drawAxes() that will draw the axes of the chart such that the axes intersect each other at (0.1, 1); see Figure 1(a). Your method must create an empty chart that looks like the one shown in Figure 1(b). Note that the pen thickness for drawing axes must be three times ticker than its default value.

 

Note. Think of appropriate constant values to use in this method, and ensure that you declare them as global named constants in your program.

 

 

(a)                                                  (b)

 

 

Figur e 1. The axes of the column chart.

 

 

 

  1. 2. The main() method. Write your main() method as follows:

 

  • First, call the drawAxes() method to draw the axes of the chart.
  • Next, using JOptionPane, ask the user to enter the number of students using an appropriate prompt.
  • Recall that the width of the StdDraw window is 1.0. Therefore, the width of the columns in your chart will vary depending  on the number of students. However,

the space between two adjacent columns may be fixed. For this program, you can

assume  that the space  between  two adjacent  columns  is always  equal  to 0.05. There will also be the same amount of space before the first column, and after the last one.

  • Define a variable columnWidth for the width of each column, and calculate its value.

 

Hint. Use the number  of students,  the space between  adjacent  columns  and the fact that the origin of the chart is at (0.1, 0.1) to calculate columnWidth.

 

  • In a for loop, prompt  the user to enter the grade  of each  student  ( an integer percentage between 0 and 100) using JOptionPane, and then call the drawColumn() method (see below) to draw the column representing the grade.

 

  • Print a message indicating the end of the program that includes your name.

 

Note. Think of appropriate constant values to use in this method, and ensure that you declare them as global named constants in your program.

 

  1. 3. The void  drawColumn(double width, int studentIndex, int grade) method.

This method should draw one column of the chart for the current  student.  The method accepts  three parameters:  the first is the width of the column,  the second  indicates  the index of this student (i.e., 1st  student, 2nd  student, and so on), and the last one is his/her grade (0-100). Write this method as follows:

 

  • First, draw the column on the chart as a filled blue rectangle.

 

Note. You need to create a formula  that works for drawing  all columns  at their correct position in the chart. To this end, use the index and grade of the student as well as the column width and the fixed space between adjacent columns.

  • Next, label  the axes using  a red pen as follows:  write the student  index  on the horizontal  axis below the column  and the student’s  grade on the vertical  axis at the  appropriate  height  depending  on  the  grade.  See  Figure  2  for  an  example, where the grades for the 1st, 2nd and 3rd students are 25, 58 and 35, respectively.

 

 

 

 

 

 

Figur e 2. An illustration of labels for each colu mn.

 

 

 

Hand in: Your Java source code, according to the instructions on page 1. Run your program twice, the first time using 4 students with grades of 43, 100, 0, and 77. The second time, use 6 students with grades of 30, 60, 90, 70, 40, and 10. Each time, use the “Save…” command in the “File” menu in the StdDraw graphics window to save the image that you get. Use the names LastnameFirstNameA3Q3Test1.jpg and  LastnameFirstNameA3Q3Test2.jpg. Hand  in these two output files.

Is this the question you were looking for? If so, place your order here to get started!