Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Console Application For this assignment you are to create a class called Lab1. Inside the class you will create three methods that will modify

Java Console Application

For this assignment you are to create a class called Lab1. Inside the class you will create three methods that will modify a three-digit int number:

sumNums - This method takes as input a three digit int and returns the sum of the three numbers. For instance, 123 would return 6 because 3 + 2 + 1 is 6

reverseNums - This method takes as input a three digit int and returns the number in reverse as a String. For instance, 123 would return "321"

getArray - This method takes as an argument a three digit number and returns the numbers as an int array. Each element of the array should contain one of the numbers

In the main method prompt for a three digit number to be input.

Create an instance of the Lab1 class

Call the sumNums method passing to it the int that was input into the Console window

Call the reverseNums method passing to it the int that was input into the Console window.

Call the getArray method passing the int that was input at the Console windows. Print the array out one number per line from the Main method using a for loop.

Your output should be the sum of the digts, the int in reverse order as a String, and the int as an array.

Your output should resemble the following:

image text in transcribed

Things to Review

Counted for loops

Arrays

Value producing methods

Things to Investigate

The Character classes toUpperCase method

The String classes charAt method

Casting

If you do not remember about assigning values you should know that you cannot simply assign a lager type to a smaller type. For instance:

double d = 3.3; int x = d;

The problem here is that a double will not fit in an int. So you need to cast.

int x = (int) d;

This tells Java to treat d as an int for the length of the expression. In this assignment you want to get the actual Unicode value from a char. This can be done by casting the char:

char c = 'A';

int x = (int) c;

x now holds the value 65 which is the Unicode value for the character 'A'

The StringBuffer Class

The StringBuffer class is a nice object when working with long strings or string that need manipulations.

You can construct a StringBuffer in serveral different ways.

StringBuffer(int capacity) - Constructs a string buffer with no characters in it and the specified initial capacity.

StringBuffer(String str) - Constructs a string buffer initialized to the contents of the specified string.

The StringBuffer has some methods for adding and inserting:

append(char c) - Appends the string representation of the char argument to this sequence.

append(String str) - Appends the specified string to this character sequence.

The append method will also allow all primitive types int, char, double, float.

You can insert into the StringBuffer using the insert method:

insert(int offset, char c) - Inserts the string representation of the char argument into this sequence.

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

Practical Azure SQL Database For Modern Developers Building Applications In The Microsoft Cloud

Authors: Davide Mauri, Silvano Coriani, Anna Hoffma, Sanjay Mishra, Jovan Popovic

1st Edition

1484263693, 978-1484263693

More Books

Students also viewed these Databases questions