I have a few things that I need to know in the AP java subset for the comp sci exam. I know it's a lot, but you guys can choose any of the topics below and explain them to me. I will really appreciate it. If you guys need more info about what I don't understand, just ask me.
First thing I don't completely understand is how to cast ints and doubles so that they are compatible with each other and not get errors. They will prob test on diff comb of these and ask which causes compile time error. What happens if you cast an int to a double, or cast a double to an int? What would get truncated? How to round up if a double is changed to an int. Also, questions will ask the result of all this with different operators, add, subtract, divide, multiply, etc.
Second, I need to know how java stores numbers in bytes. I do not understand the byte system at all. 1 for positive, 0 for negative. Largest number can be 2^7 - 1 in type byte. Int stores 4 bytes (32 bits). I also need to know the Interger.MIN_VALUE and Interger.MAX_VALUE and how much can it hold. Only one I need to know all this for in the int type. Yeah, so I don't understand any of the 32, and eight bits, etc. It's so confusing.
I also need to know the same as above about double type, not float. Sign * Mantissa * 2exponent. I don't understand any of it. Also need to know about round off errors.
Also, I need to know how to read hexa-decimal numbers and how to convert them to and from binary.
I kinda understand final variables which are constant and can't be changed but that's all I know. Is there anything else I should know about these?
Will post more, you guys choose whatever you wanna answer/explain/clarify.
30 replies to this topic
#1
Posted 05 April 2011 - 06:51 PM
|
|
|
#2
Posted 06 April 2011 - 09:36 AM
Quote
First thing I don't completely understand is how to cast ints and doubles so that they are compatible with each other and not get errors. They will prob test on diff comb of these and ask which causes compile time error. What happens if you cast an int to a double, or cast a double to an int? What would get truncated? How to round up if a double is changed to an int. Also, questions will ask the result of all this with different operators, add, subtract, divide, multiply, etc
When you cast an int to a double, nothing special happens. When you cast double to int, it just drops all the decimals, no rounding whatsover. 3.9 ->int = 3.
Rounding etcetera is done using the Math class (java.lang.Math)
You can do Math.floor(), Math.ceil() and Math.round()
Round() rounds like the average person would do it, 3.0-3.4 becomes 3, 3.5-3.9 becomes 4.
Quote
Second, I need to know how java stores numbers in bytes. I do not understand the byte system at all. 1 for positive, 0 for negative. Largest number can be 2^7 - 1 in type byte. Int stores 4 bytes (32 bits). I also need to know the Interger.MIN_VALUE and Interger.MAX_VALUE and how much can it hold. Only one I need to know all this for in the int type. Yeah, so I don't understand any of the 32, and eight bits, etc. It's so confusing.
Example number: 1345
1 3 4 5 ----------------------- 10³ 10² 10^1 10^0 1000 100 10 1So to have the total number, you actually do:
(1*1000)+(3*100) + (4*10) + (5*1) = 1345
The numbers above can range from 0-(base-1), so numbers from 0-9.
The binary system is exactly the same (Yay :) )
The numbers above can range from 0-(base-1), so numbers from 0-1.
Our sample number is: 10100110
1 0 1 0 0 1 1 0 2^7 2^6 2^5 2^4 2³ 2² 2^1 2^0 128 64 32 16 8 4 2 1So the value of 10100110 is:
(1*128) + (0*64) + (1*32) + (0*16) + (0*8) + (1*4) + (1*2) + (0*1)
= 128 + 32 + 4 + 2 = 166
If an integer can hold 32 bits (=4bytes), then it can hold 32x1
11111111111111111111111111111111
That are 32 ones ^^
Now, if you go calculate how big the number is that that can hold. You'll see it can hold double the amount that java gives if you call Integer.MAX_VALUE.
That is because the first bit, is used as + or -, So if you wish to know the value an integer can hold, it's 2^(32-1)-1.
Assume an integer would be 2 bits big, the first is used only to determine + or -. So that's leaves us 1 bit to store a number.
result: the max value = 2^(2-1) -1 = 1
That's why in some languages (not sure if Java is part of those) have "unsigned ints", that means they are always positive, got no sign, and thus can use the first bit to help storing the number. Unsigned can always hold a number with a maximum of 2x signed maximum.
Quote
I also need to know the same as above about double type, not float. Sign * Mantissa * 2exponent. I don't understand any of it. Also need to know about round off errors.
Quote
Also, I need to know how to read hexa-decimal numbers and how to convert them to and from binary.
If you understood my binary explanation, this shouldn't be too hard.
Hex numbers can range from 0-(base-1) , so 0-15.
Now, because it's a bit hard to read if we'd just go on after 9 (101, is it 10,1 or 1,0,1?) We go use the alphabet beyond 9, so
0123456789ABCDEF
Hex to bin:
I think it's easiest to first transform from hex to decimal, and from decimal to binary.
Gonna take a smaller number as sample: 1A1
1 A 1 16² 16^1 16^0 256 16 1so (1*256) + (A=10 *16) + (1*1) = 417
Then from 417(decimal) to binary.
First, search for the lowest number you can use as power to base 2 that's lower than 417.
1, 2, 4, 8, 16, 32, 64, 128, [B][COLOR="lime"]256[/COLOR][/B], [COLOR="red"]512, 1024[/COLOR] = 2^power 0, 1, 2, 3, 4, 5, 6, 7, [B][COLOR="lime"]8[/COLOR][/B], [COLOR="red"]9, 10[/COLOR] = powerWe see that 2^9 = 512 and thus bigger than 417, 2^8 is 256 and smaller.
Now that we know that, we know we need 8+1 bits:
? ? ? ? ? ? ? ? ? 2^8 2^7 2^6 2^5 2^4 2³ 2² 2^1 2^0 256 128 64 32 16 8 4 2 1Start by setting the first bit to 1:
1 ? ? ? ? ? ? ? ? 2^8 2^7 2^6 2^5 2^4 2³ 2² 2^1 2^0 256 128 64 32 16 8 4 2 1Total number = 256 now, deduct it from 417:
417-256=161
Now, just go to the right bit by bit, if the number (Bottom row) of that bit is smaller, set bit to 1, if it's bigger: set to 0, if it's equal set to 1. Also keep deducting if you set to 1.
next number: 128, smaller than 161? yes: bit to 1
161-128=33
1 1 ? ? ? ? ? ? ? 2^8 2^7 2^6 2^5 2^4 2³ 2² 2^1 2^0 256 128 64 32 16 8 4 2 1next number: 64, smaller than 33? no: bit to 0
total remains 33
1 1 0 ? ? ? ? ? ? 2^8 2^7 2^6 2^5 2^4 2³ 2² 2^1 2^0 256 128 64 32 16 8 4 2 1
next number: 32, smaller than 32? yes: bit to 1
33-32=1
1 1 0 1 ? ? ? ? ? 2^8 2^7 2^6 2^5 2^4 2³ 2² 2^1 2^0 256 128 64 32 16 8 4 2 1
I'm gonna skip some steps now ^^
1 1 0 1 0 0 0 0 ? 2^8 2^7 2^6 2^5 2^4 2³ 2² 2^1 2^0 256 128 64 32 16 8 4 2 1
next number: 1, smaller than 1? no, but it's equal: bit to 1
1-1=0
1 1 0 1 0 0 0 0 1 2^8 2^7 2^6 2^5 2^4 2³ 2² 2^1 2^0 256 128 64 32 16 8 4 2 1so 1A1(Hex) is 110100001(binary) is 417(decimal)
I hope you can figure out your way for the other way around.
Quote
I kinda understand final variables which are constant and can't be changed but that's all I know. Is there anything else I should know about these?
private final int number;
public void setNumber(int number){
this.number = number;
}
That is valid code. Just know the setter will only work once, and fail after.Maybe IDE's gonna warn you about the danger of this setter. But it should compile.
#3
Posted 19 April 2011 - 04:08 PM
Whoa, super helpful post WimDC. I didn't think anyone would answer everything. Sorry for replying so late. Went on Spring break and completely forgot about programming after, lol.
I have a new questions. I need someone to clarify the difference between primitive and object types.
Also, how does an object reference get copied. For example, for primative, if you declare two ints, int a, b;
a = 1;
b = a;
It creates a new slot for it and adds one to b's value, right?
But for objects, I read somewhere it works differently. Would someone explain how?
I have a new questions. I need someone to clarify the difference between primitive and object types.
Also, how does an object reference get copied. For example, for primative, if you declare two ints, int a, b;
a = 1;
b = a;
It creates a new slot for it and adds one to b's value, right?
But for objects, I read somewhere it works differently. Would someone explain how?
#4
Posted 19 April 2011 - 06:13 PM
#5
Posted 19 April 2011 - 11:11 PM
Thanks Lethal, will post more questions soon.
#6
Posted 23 April 2011 - 09:46 AM
Okay, new question guys. I need to know how to use and why to use the comparable interface. I know you use it when you're comparing two objects and to add the compareTo method, but I don't understand why we use that interface instead of using creating a regular method of compareTo in our class like we do. What's the point of implementing that interface?
Edit: This is the RAW comparable that is going to be tested on the test. And I just saw that you can make comparable methods which take to comparable objects and find the smallest temperature object.
That confuses me even more. I see the compareTo from the comparable interface. But where did the comparable objects come from (in parameters). I thought interfaces couldn't create objects.
Edit: This is the RAW comparable that is going to be tested on the test. And I just saw that you can make comparable methods which take to comparable objects and find the smallest temperature object.
public static Comparable min(Comparable a, Comparable b)
{
if (a.compareTo(b) < 0)
return a;
if ([opposte])
return b;
}
That confuses me even more. I see the compareTo from the comparable interface. But where did the comparable objects come from (in parameters). I thought interfaces couldn't create objects.
#7
Posted 23 April 2011 - 10:41 AM
Okay, I'll take a crack at explaining this.
Comparable objects, by and large, presume an absolute ordering between any two elements. An example of an "absolute ordering" would be the numbers in the number line. 1 is always less than 5, in all cases, and the same goes for any element in an absolute ordering. If you were to say the absolute order of the days of the week is [Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday], then Tuesday is always less than Friday in all cases as well. So when you perform a comparison between two objects using the compareTo method, you should get a value representing the difference between the absolute position of element a as compared to element b when you code it as such: a.compareTo(b). The returned value is always where A is in relation to B, so if B is 25 and A is 15, you will get -10, since A is -10 from B. Simple, no?
Since Comparable is an interface, any class may implement the Comparable interface, and as such you can determine (using your static method) which of two objects that are instances of a class that implements the Comparable interface is the minimum of the two. Imagine we made a DayOfWeek object implementing the Comparable interface:
Comparable objects, by and large, presume an absolute ordering between any two elements. An example of an "absolute ordering" would be the numbers in the number line. 1 is always less than 5, in all cases, and the same goes for any element in an absolute ordering. If you were to say the absolute order of the days of the week is [Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday], then Tuesday is always less than Friday in all cases as well. So when you perform a comparison between two objects using the compareTo method, you should get a value representing the difference between the absolute position of element a as compared to element b when you code it as such: a.compareTo(b). The returned value is always where A is in relation to B, so if B is 25 and A is 15, you will get -10, since A is -10 from B. Simple, no?
Since Comparable is an interface, any class may implement the Comparable interface, and as such you can determine (using your static method) which of two objects that are instances of a class that implements the Comparable interface is the minimum of the two. Imagine we made a DayOfWeek object implementing the Comparable interface:
public class DayOfWeek implements Comparable<DayOfWeek>
{
public DayOfWeek(String day)
{
// Assume theDays is a List<String> that contains all the days of the week in
// absolute order as described above in this post.
if (!theDays.contains(day))
{
throw new IllegalArgumentException();
}
this.day = day;
}
public int compareTo(DayOfWeek other)
{
return theDays.indexOf(day) - theDays.indexOf(other.day);
}
@Override
public String toString()
{
return day;
}
private String day;
}
From this, you can now place two DayOfWeek objects into your min method, and determine which one is the minimum! public static void main(String[] args)
{
String first = "Tuesday", second = "Friday";
DayOfWeek tue = new DayOfWeek("Tuesday"),
fri = new DayOfWeek("Friday");
System.out.println("Which is smaller, Tuesday or Friday?");
System.out.println("The smaller is: " + AnAliensStaticClass.min(tue, fri));
}
This is also how you create objects that are "Comparable" objects. They're just objects that implement the interface, you don't create a Comarable directly. :)
Wow I changed my sig!
#8
Posted 23 April 2011 - 11:04 AM
Okay, so I have to include: implements Comparable<DayOfWeek> inorder to create a compareTo method?
Also, is <DayOfWeek> part for the arraylist, i'm not sure cause i haven't learned arraylists yet.
Also, is <DayOfWeek> part for the arraylist, i'm not sure cause i haven't learned arraylists yet.
#9
Posted 26 April 2011 - 03:00 PM
What's with comparable objects? Does that mean that they are the same type of object and you can use compareTo on them?
Also, can someone explain recursion with two dimensions. Having a hard time understanding it.
Also, can someone explain recursion with two dimensions. Having a hard time understanding it.
#10
Posted 26 April 2011 - 03:24 PM
I'm gonna assume these "comparable objects" have implemented the comparable interface.
All this does is allow your objects to be compared to each other.
There are many ways to compare objects/dates/numbers/letters/etc.
When you define something like:
HOW they are compared will be defined in your compareTo method.
Without this method how would you even dare to compare your objects?
On this website they are creating Employees that implement the Comparable interface.
Java Comparable Example | Java Tutorials and Examples
They compare the Employee objects by comparing the age variable.
What's the use?
Imagine you have 4,000,000 employees. Now you want to create a sorted list of employees with their age being the key factor.
Since you've implemented the comparable interface you can easily create your own sort method or add them to an ArrayList call the sort method from the Collections class.
All this does is allow your objects to be compared to each other.
There are many ways to compare objects/dates/numbers/letters/etc.
When you define something like:
class Number implements Comparable<Number> {
...
}
it means you are able to compare your numbers SOMEHOW. HOW they are compared will be defined in your compareTo method.
Without this method how would you even dare to compare your objects?
<Number>This small part means that when you call the compareTo method, you MUST compare another Number.
On this website they are creating Employees that implement the Comparable interface.
Java Comparable Example | Java Tutorials and Examples
They compare the Employee objects by comparing the age variable.
What's the use?
Imagine you have 4,000,000 employees. Now you want to create a sorted list of employees with their age being the key factor.
Since you've implemented the comparable interface you can easily create your own sort method or add them to an ArrayList call the sort method from the Collections class.
#11
Posted 26 April 2011 - 03:30 PM
Alright thanks Lethal. Anyone know about recursion?
#12
Posted 26 April 2011 - 03:32 PM
I'm confused by the two dimensional recursion... but, I think you may be referring to tree recursion. The best example I could think of is recursion with Fibonacci numbers.
I'd read this(Mainly section 18.1):
Programming via Java: Recursion examples
In short, every recursive call leads to two more recursive calls, until the base case has been reached. Be sure to notice all of the unnecessary repeated calls.
This is bad in terms of running time and memory.
I can't remember the math but I know this version of computing fibonacci runs in exponential time. O(2^N)
I'd read this(Mainly section 18.1):
Programming via Java: Recursion examples
In short, every recursive call leads to two more recursive calls, until the base case has been reached. Be sure to notice all of the unnecessary repeated calls.
This is bad in terms of running time and memory.
I can't remember the math but I know this version of computing fibonacci runs in exponential time. O(2^N)
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









