Question
CIS 180 Lab 10 Static Methods Be sure to read through the instructions COMPLETELY before beginning. Prep: As we discussed in class, static attributes are
CIS 180 Lab 10
Static Methods
Be sure to read through the instructions COMPLETELY before beginning.
Prep:
As we discussed in class, static attributes are shared memory attributes that are the SAME for all instances of that class. Since they are shared amongst all instances, it becomes pointless to specify a specific instance in order to refer to it. As such we can simply use the class name to reference it.
Example of a public static int attribute being accessed: ClassName.thisIsAStaticAttribute = 5;
Methods can also be static in nature, and can also be used in a similar fashion.
Example: ClassName.thisIsAStaticMethod();
Creating static methods in this fashion can be particular advantageous when the method does not need to use non-static attributes. Recall with the Math class you did not need to create an instance of Math. Why would you, math is math, you cant change how it works. Creating an instance of Math is an unnecessary step just to use the methods. This is why the Math class is comprised mostly of static methods.
Example: Math.abs(-3);
In this weeks lab youre going to create a class that has 3 static methods and nothing else. As such we should make the default constructor private, to prevent anyone from making an instance of this class. It would be a waste of memory and waste of processing time.
IntegerConverter class
In this class you will create a class called IntegerConverter. IntegerConverter is going to be a class very similar to the Math class in the sense that it will be a useful tool. Its purpose is to provide methods that will take an integer as an argument and convert it to some other String based form. Below I will explain the three forms:
Hexadecimal
Hexadecimal is known as a base 16 counting system.Our decimal system is base 10, in the sense that we could from 0 to 9 in the ones place, then make a 1 in the tens place, and start counting 0 to 9 again in the ones spot.Hexadecimal works the same way except we could from 0 to F (where A = 10, B = 11, C = 12, D = 13, E = 14, F = 15).For instance, 16 E = 14, F = 15).For instance:
15 in hex is F
16 in hex is 10
17 in hex is 11
18 in hex is 12
Etc.
I have actually provide the code already for this so you can see the process to convert from base 10 (decimal) to base 16 (hexadecimal).It involves dividing (integer division) the base 10 number by 16, where the remainder gives us a one of the digits of the hex value.You then divide the base 10 number again and use the remainder as the next digit of the hex value.You continue this until the base 10 number is 0.The last step is to reverse the order of the digits as you got them.The process I just describes if you the digits in reverse order from what they should be.Luckily java provided me with a way to reverse the String.
package lab9; public class IntegerConverter { public static String toHex(int number) { String total = ""; //If the number is negative, make it positive if(number<0) { number = number*-1; } do{ int value = number % 16; //If the value of 'value' is greater than 9, assign the appropriate letter switch(value) { case 10: total+="A"; break; case 11: total+="B"; break; case 12: total+="C"; break; case 13: total+="D"; break; case 14: total+="E"; break; case 15: total+="F"; break; default: total+=value; } //divide out 16 and repeat number=number/16; } while(number!=0); //reverse the string total = (new StringBuilder(total)).reverse().toString(); return total; } public static String toBinary(int number) { String total = ""; //Fill in the code here: return total; } public static String toRomanNumeral(int number) { /* * I = 1 * IV = 4 * V = 5 * IX = 9 * X = 10 * XL = 40 * L = 50 * XC = 90 * C = 100 * CD = 400 * D = 500 * CM = 900 * M = 1000 * * Rules * the numeral I can be placed before V and X to make 4 units (IV) and 9 units (IX) respectively * X can be placed before L and C to make 40 (XL) and 90 (XC) respectively * C can be placed before D and M to make 400 (CD) and 900 (CM) according to the same pattern * * */ String total = ""; //Fill in the code here: return total; } }
Binary
If Hex is base 16, decimal is base 10, then binary is known as base 2.With binary we only could 0 to 1.For example.
0 in binary is 0
1 in binary is 1
2 in binary is 10
3 in binary is 11
4 in binary is 100
5 in binary is 101
6 in binary is 110
7 in binary is 111
8 in binary is 1000
Etc.
To convert from decimal to binary is almost the same exact process as convert from decimal to hex, except instead of dividing by 16, you will divide by 2.The other nice thing is, I will not need that switch statement to substitute letters in for values larger than 10 (we will never have a remainder that high dividing by 2).
Roman Numerals
Converting from decimal to roman numerals is a bit of a different process.Roman numerals assign letters to values instead of numbers.Below you will see a listing of what each roman numeral represents:
* I = 1
* IV = 4
* V = 5
* IX = 9
* X = 10
* XL = 40
* L = 50
* XC = 90
* C = 100
* CD = 400
* D = 500
* CM = 900
* M = 1000
In order to convert a number from decimal to roman numerals, I need to start at the bottom of the list and work my way up. I ask myself if I can subtract 1000 from it without the result being negative.If I can, then I would write down an M (or in this case add it to my string) and subtract out the 1000.Ask myself again if I could subtract out 1000 without the result being negativeif I cant, then I move on and try 900 until I cant.
Consider the value 2952.
2952 1000 = 1952(Add M to my string)
1952 1000 = 952 (Add M to my string)
952 1000 = Negative, move on to 900
952-900 = 52 (Add CM to my string)
52 900 = Negative, move on to 500
52 500 = Negative, move on to 400
52 400 = Negative, move on to 100
52 100 = Negative, move on to 90
52 90 = Negative, move on to 50
52 50 = 2 (Add L to my string)
2 50 = Negative, move on to 40
2 40 = Negative, move on to 10
2 10 = Negative, move on to 9
2 9 = Negative, move on to 5
2 5 = Negative, move on to 4
2 4 = Negative, move on to 1
2 1 = 1 (Add I to my string)
1 1 = 0 (Add I to my string)
0 (Finished)
My string now contains MMCMLII.
I have provided you with the class and one of the methods already complete. Please fill in the other two methods. In the case of negative numbers simply convert the number to its positive equivalent (absolute value). Create a second class that has the main method in it. In the main method, create a menu system that asks the user for a number and then displays the number (in main, not the methods) as hexadecimal, binary, and roman numeral.
Check-off points:
Make sure you commented everything. All loops, methods, and attributes. Also take credit for your work (Name, Date, description of the class) at the top of the each class.
If you believe you have completed this lab, have the TA check you off. Once you are checked off for all portions, upload all .java files (only!) to myCourses.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started