Java Console Application For this assignment you are to crea
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:
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.
Solution
Lab1.java
import java.util.Scanner;
public class Lab1 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println(\"Enter a three digit number: \");
int n = scan.nextInt();
Lab1 l = new Lab1();
System.out.println(\"Sum of digits is \"+l.sumNums(n));
System.out.println(\"Reverse Number is \"+l.reverseNums(n));
System.out.println(\"Array elements are \");
int a[] = l.getArray(n);
for(int i=a.length-1; i>=0; i--){
System.out.println(a[i]);
}
}
public int sumNums(int n){
int sumOfDigits = 0;
while(n>0){
int r = n % 10;
n = n/10;
sumOfDigits = sumOfDigits + r;
}
return sumOfDigits;
}
public String reverseNums(int n){
String s = String.valueOf(n);
char temp = s.charAt(0);
StringBuffer sb = new StringBuffer(s);
sb.reverse();
return sb.toString();
}
public int[] getArray(int n){
int n1 = n;
int count = 0;
while(n>0){
count++;
n = n/10;
}
int a[] = new int[count];
count = 0;
while(n1>0){
int r = n1 % 10;
a[count] = r;
n1 = n1/10;
count++;
}
return a;
}
}
Output:
Enter a three digit number:
123
Sum of digits is 6
Reverse Number is 321
Array elements are
1
2
3


