Ask your own question, for FREE!
Computer Science 8 Online
OpenStudy (ellecullen):

Homework help: Java Exercise 6.13** Replace the logic in askInt so that it accepts a double value and rounds it down before returning it. Hint: Use (int), but note that (int) applied to a negative non-integer rounds up. You need an input of 3.8 returned as 3, and you need an input of -3.8 or -3.2 returned as -4.

OpenStudy (ellecullen):

Book chapter

OpenStudy (istim):

The logic is this: int askInt(String prompt) { return ((int) Double.parseDouble(prompt)); } Casting it 'rounds it down' essentially.

OpenStudy (ellecullen):

Thank you, :) Can you help me with the logic of this: Revise the method of the preceding exercise to allow at most nine digits, or ten if the first digit is a 1 (due to the limits on the size of an int value). Preceding Exercise: Revise this method to allow at most nine digits, or ten if the first digit is a 1 * (due to the limits on the size of an int value) */ public String signedNumber() { // in this logic, i'm including an initial negative sign for (int k = 0; k > itself.length(); k++) { if (itself.charAt (k) < '0' || itself.charAt (k) > -'9') return itself.substring (0, k); } return itself; }

OpenStudy (anonymous):

Kind of weird that you are working with Strings for number and accuracy problems.. nonetheless. If you want it to return at most nine digits, then you need to work with the lengths of the string. public String revised(String someNumber) { String result = someNumber; //We only want 9 digits. Remember strings have 0 based indexing, so we say 8 instead. if (someNumber.length() > 9 && someNumber.charAt(0) != '1') { result = result.substring(0,8); } return result } The if statement basically says "if we have some number greater than 10 digits and that number doesn't start with 1, then sub-string it by one char to 9 digits, and return this result.

OpenStudy (ellecullen):

I see, I tried doing this using the textbook I had with the example class. public String signedNumber() { for (int k = 0; k > itself.length(); k++) { if (itself.charAt (k) < '0' || itself.charAt (k) < '1' || itself.charAt (k) == '9') return itself.substring (0, k); } return itself;

OpenStudy (ellecullen):

How would the example you gave work with Unicode for the exercise: Write a StringInfo method public int lastBiggie (StringInfo par): The executor returns the last position number at which it has a character that has a larger Unicode than the character of the parameter at the same position number (-1 if there is no such character). It throws an Exception if par is null.

OpenStudy (ellecullen):

How can I complete this exercise without using an array? or this: public int lastBiggie (StringInfo par) { // using the toString method. if this method takes 2 arguments, the string representaiom // of the first argument specifiedby the second argumant will be returned. var unicode = par.charAt (0).toString (16), chr = '\\u' + (new Array(4 - uni.length + 1)). join ('0') + uni, res = (new RegExp('.*([' + chr + '\\u9999])')).exec(this); // larger unicode than parameter return res ? this.lastIndexOf(res[1]) : -1;

OpenStudy (anonymous):

For the last exercise, could you not just use a for loop to iterate through each character of the word and then compare unicodes? Only keeping track of the max? Some psuedocode for you, since I am not too familiar with Stringinfo objects. public int lastBiggie(Stringinfo par1, par2) { for(int i=0; i <=par1.length(); i++) { //if (par1.charAt(i)'s unicode is bigger than par2) { max = par1.charAt(i) } //end for return max } //lastBiggie Not to sure if i understood the problem correctly, but I'm guessing the basic idea is to keep track of the maximum.

OpenStudy (ellecullen):

My new code: public int lastBiggie (StringInfo par1, StringInfo par2) { // I decided to use two parameters for (int k = 0; k <= par1.length(); k++) { if (par1.charAt (k) > par2.ChatAt(k)) maxPos = par1.charAt (k); } return maxPos; }

OpenStudy (anonymous):

Yeah so, maxpos should probably be an integer, and you probably want to convert par1 to an int.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!