Question

Need help with implementing a While, Do-while and for loops in a program. UML: - scnr...

Need help with implementing a While, Do-while and for loops in a program.

UML:

- scnr : Scanner

- rows : int

- MAX_ASCII : int

-------------------------------------------------------------------------------

+ CharacterTables() :

+ CharacterTables(rows : int) :

- getStartingValue() : int

-displayTable(startValue : int) : void

- userContinue() : boolean

+ main(args : String[]) : void

In Section 5.10 we saw that there is a close relationship between char and int. Variables of type char store numbers. (Actually, variables of all types store numbers - that’s what’s in computer storage: 0s and 1s.) In Java, you can do something like this:

char letter = 65;

System.out.println(letter);

and you will see the letter ‘A’ printed. (See Table 5.10.1) On the other hand, you can’t do this:

int x = 65;

char letter = x;             // will cause an error

System.out.println(letter);

because the compiler cannot verify that the x won’t be a value that is out of bounds for a char. But you can force the conversion with a cast (section 5.8) this way:

int x = 65;

char letter = (char) x;

System.out.println(letter);

if you’re sure (like here) that x will be a valid char value.

In this lab, you will print a small table of corresponding int and char values. (See the sample run.)

  1. Create a class named CharacterTables as specified in the UML.

  2. Create a constant for the max ASCII value (126.)

  3. The main method will have only one line:
    new CharacterTables();

  4. The no-arg constructor calls the 1 arg constructor with an argument of 10.

  5. The 1-arg constructor does the work of the application, calling the three private methods in a loop until the user enters ‘Q’. See the sample run.

  6. The getStartingValue method has an endless loop. It prompts for the starting value, which must be between 0 and (MAX_ASCII - rows + 1) so that the table does not extend beyond the ASCII table. Return only when a valid value has been entered.

  7. The displayTable method will print the table shown, in a loop. Each row has a number and the corresponding character. The first row will be the user’s number and corresponding letter, the second row will be the number+1, etc.

  8. The userContinue method prompts the user to continue or quit. It returns false on entry of ‘Q’ or ‘q’, true otherwise.

  9. When a loop is required , you must determine the correct type of loop to use.

  10. // Table must be printed using (printf), Only use While, Do-While and For loops in this exercise.

Sample run

User input in bold.

Enter a starting integer between 0 and 117: -1

Enter a starting integer between 0 and 117: 118

Enter a starting integer between 0 and 117: 117

117 u

118 v

119 w

120 x

121 y

122 z

123 {

124 |

125 }

126 ~

Enter C to continue, Q to quit: c

Enter a starting integer between 0 and 117: 61

61 =

62 >

63 ?

64 @

65 A

66 B

67 C

68 D

69 E

70 F

Enter C to continue, Q to quit: q

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Source Code in Java:

import java.util.Scanner;
class CharacterTables
{
//class variables
Scanner scnr;
int rows;
final int MAX_ASCII=126; //constant variable to store maximum ASCII value
CharacterTables(int rows) //parametrized constructor
{
this.rows=rows;
scnr=new Scanner(System.in);
}
CharacterTables() //default constructor
{
this(10);
}
int getStartingValue() //method to input starting value till it is valid and return it
{
int startValue;
do
{
System.out.print("Enter a starting integer between 0 and "+(MAX_ASCII-rows+1)+": ");
startValue=scnr.nextInt();
}while(startValue<=0 || startValue>MAX_ASCII-rows+1);
return startValue;
}
void displayTable(int startValue) //method to display a table of characters from a given starting value
{
for(int i=startValue;i<startValue+rows;i++)
{
System.out.printf("%d %c\n",i,i);
}
}
boolean userContinue() //method to accept user choice to continue or not and return a boolean accordingly
{
System.out.print("Enter C to continue, Q to quit: ");
char ch=scnr.next().charAt(0);
if(ch=='q' || ch=='Q')
return false;
return true;
}
public static void main(String args[])
{
//running the methods
CharacterTables ct=new CharacterTables();
do
{
int startValue=ct.getStartingValue();
ct.displayTable(startValue);
}while(ct.userContinue());
}
}

Output:

Add a comment
Know the answer?
Add Answer to:
Need help with implementing a While, Do-while and for loops in a program. UML: - scnr...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hi I need help with a java program that I need to create a Airline Reservation...

    Hi I need help with a java program that I need to create a Airline Reservation System I already finish it but it doesnt work can someone please help me I would be delighted it doesnt show the available seats when running the program and I need it to run until someone says no for booking a seat and if they want to cancel a seat it should ask the user to cancel a seat or continue booking also it...

  • In basic c develop the code below: (a) You will write a program that will do...

    In basic c develop the code below: (a) You will write a program that will do the following: prompt the user enter characters from the keyboard, you will read the characters until reading the letter ‘X’ You will compute statistics concerning the type of characters entered. In this lab we will use a while loop. We will read characters from stdin until we read the character ‘X’. Example input mJ0*5/]+x1@3qcxX The ‘X’ should not be included when computing the statistics...

  • In this assignment, we will be making a program that reads in customers' information, and create...

    In this assignment, we will be making a program that reads in customers' information, and create a movie theatre seating with a number of rows and columns specified by a user. Then it will attempt to assign each customer to a seat in a movie theatre. 1. First, you need to add one additional constructor method into Customer.java file. Method Description of the Method public Customer (String customerInfo) Constructs a Customer object using the string containing customer's info. Use the...

  • JAVA Only Help on the sections that say Student provide code. The student Provide code has...

    JAVA Only Help on the sections that say Student provide code. The student Provide code has comments that i put to state what i need help with. import java.util.Scanner; public class TicTacToe {     private final int BOARDSIZE = 3; // size of the board     private enum Status { WIN, DRAW, CONTINUE }; // game states     private char[][] board; // board representation     private boolean firstPlayer; // whether it's player 1's move     private boolean gameOver; // whether...

  • 6.15 Program 6: Using Arrays to Count Letters in Text 1. Introduction In this program, you...

    6.15 Program 6: Using Arrays to Count Letters in Text 1. Introduction In this program, you will practice working with arrays. Your program will read lines of text from the keyboard and use an array to track the number of times each letter occurs in the input text. You will use the contents of this array to generate a histogram (bar graph) indicating the relative frequencies of each letter entered. Test cases are available in this document. Remember, in addition...

  • I NEED HELP WITH DEBUGGING A C PROGRAM! PLEASE HEAR ME OUT AND READ THIS. I...

    I NEED HELP WITH DEBUGGING A C PROGRAM! PLEASE HEAR ME OUT AND READ THIS. I just have to explain a lot so you understand how the program should work. In C programming, write a simple program to take a text file as input and encrypt/decrypt it by reading the text bit by bit, and swap the bits if it is specified by the first line of the text file to do so (will explain below, and please let me...

  • Please do this in C++ using only for loops, while loops, and do while loops. No...

    Please do this in C++ using only for loops, while loops, and do while loops. No arrays. Use for loop, while loop, and do while loops to generate a table of decimal numbers, as well as the binary, octal, and hexadecimal equivalents of the decimal numbers in the range 1-256 Note: See Appendix for number systems The program should print the results to the Console without using any string formats Design Details: The Main program should prompt the user for...

  • Program is in C++, program is called airplane reservation. It is suppose to display a screen...

    Program is in C++, program is called airplane reservation. It is suppose to display a screen of seating chart in the format 1 A B C D E F through 10. I had a hard time giving the seats a letter value. It displays a correct screen but when I reserve a new seat the string seats[][] doesn't update to having a X for that seat. Also there is a file for the struct called systemUser.txt it has 4 users...

  • Intro to Programming in C – Large Program 1 – Character/ Number converter Assignment Purpose: To...

    Intro to Programming in C – Large Program 1 – Character/ Number converter Assignment Purpose: To compile, build, and execute an interactive program with a simple loop, conditions, user defined functions and library functions from stdio.h and ctype.h. You will write a program that will convert characters to integers and integers to characters General Requirements • In your program you will change each letter entered by the user to both uppercase AND lowercase– o Use the function toupper in #include...

  • The school needs a program to organize where the musicians will stand when they play at...

    The school needs a program to organize where the musicians will stand when they play at away games. Each away stadium is different, so when they arrive the conductor gets the following information from the local organizer: The number of rows they have to stand on. The maximum number of rows is 10. The rows are labelled with capital letters, 'A', 'B', 'C', etc. For each row, the number of positions in the row. The maximum number of positions is...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT