Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Characters and Integers Create a program in a class named Lab2Characters that takes a character from the user and outputs the corresponding ASCII value, as

image text in transcribedimage text in transcribed

Characters and Integers Create a program in a class named Lab2Characters that takes a character from the user and outputs the corresponding ASCII value, as well as displaying some sample characters and their ASCII values. Use the Scanner class in a main() method to get the user input character one time, store that input in a char variable, and pass it as an argument to a (static) method you write that returns its ASCII value as an int and print the result. Then, print out the characters that would give the output below, all on one line separated by spaces. You can just do this with one String if you'd like, e.g. System.out.println("E xample");. Finally, run your method on those characters and print the results to verify. Your output should match what's below. Hint: use System.out.print() to print multiple results on the same line. 74 65 78 50 48 50 50 106 97 118 97 58 68 Here's some useful information about this exercise: You have learned about integers and the type int so far. Java, being a C-family language, can represent letters, digits, and a considerable variety of special symbols using ints. Every character has a corresponding integer representation. The set of characters a computer uses together with the corresponding integer representations for those characters is called that computer's character set, and in this case, we're using ASCII. You can determine an integer's character equivalent by preceding that integer with (char), as in (char) 80, which would be 'P', and vice versa using (int). This process of using some Java type in parentheses next to an expression is called type casting. An ASCII table is linked in the Readings and Reference Material section. String Methods Next week you'll be learning about Strings in Java, but in lecture you have already seen some usage of String instance methods. Strings are basically sequences of characters, but for this exercise you don't need to understand exactly how they work. You'll be using String methods and basic arithmetic to do some comparisons of Strings. For this program, follow the approach given below: 1. Design: Design your program by writing pseudocode, describing the distinct steps that your program will need to perform. You can write it as comments in your Java file. 2. Implement: Write the Java code that will perform the task. Create a program in a class named StringMultiples that has a method with the following method header: public static boolean checkIfMultipleof(String single, String multiple). Given a String single and another String multiple, the method will return whether or not single concatenated with itself some number of times is the same as multiple. More formally, the method returns true if and only if there exists a positive, non-zero integer x such that single concatenated with itself x times equals multiple, where single is a non-empty String and multiple is any String. Concatenation is the merging of two or more Strings into one. See the example usages below: checkIfMultipleOf("cat", "catcatcat"); // returns true checkIfMultipleOf("cat", "catdatcat"); // returns false checkIfMultipleOf("cat", "cat"); // returns true checkIfMultipleOf("cat", "catcatcat"); // returns false You will need to use methods from the String class that you're probably not yet familiar with. Before you begin planning your solution, we'd recommend referencing the Java API documentation on the String class to see what methods are available to you, which is linked in the Reference Material section. Your solution should not include loops, indexing, or substrings, so you can rule those kinds of String methods out. Also remember that comparison of Strings should always be done using the .equals() method, not ==. Once you've written your checkIfMultipleof() method, write a main() method for the class that asks the user to input two Strings, one at a time, and runs your method on those Strings and prints the result. Afterwards, print the results of several method calls to verify that your solution works. You can use the example usage shown above as a starting point, but you should have more than just that. Think: what part of the description above is not tested by those three method calls? Are there any edge-case valid inputs? This is the idea of comprehensive testing, which will be important for future assignments

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases Illuminated

Authors: Catherine M Ricardo, Susan D Urban

3rd Edition

1284056945, 9781284056945

More Books

Students also viewed these Databases questions

Question

a. What is the purpose of the team?

Answered: 1 week ago

Question

b. What are its goals and objectives?

Answered: 1 week ago