Wednesday, January 30, 2013

Lesson 21 Excercise


Lesson 21 Exercise
Matthew Walker
Balcar 3A 1/30/13
  1. The classes that convert primitives to objects are called Wrapper classes.

  1. Name the four primitive data types with which wrapper classes primarily deal.
Integer, Double, Boolean,Character

3. Write code that will convert double dx into a wrapper class object. Call the object dd.
   

4. Write code that will produce a Boolean type wrapper object called bj (“wrap” a true
inside it).
    boolean bj = bObj.booleanValue( 5<7);

5. Write code that will convert the integer ip into an Integer wrapper class object. Call the
object ozzie.

6. Assume you have the object Character cw. Write code to convert this to a primitive
character.

7. Assume you have Double objects d1 and d2. Show how you would multiply the values
stored in these objects and then store the answer in primitive double dd.

8. Assume you have Integer objects i1 and i2. Show how you would add the values stored
in these objects and then store the answer in a third Integer object called i3.

9. Write code that will extract the boolean wrapped in the Boolean wnOh and test it with an
if statement.
if ( )

10. Convert the object jj (of type Double) into a primitive float called ff.

11. Convert the object pk (of type Double) into a primitive int called gurg. What is the
danger of doing this?

12. What is the primary purpose of wrapper classes?

Lesson 20 Excercise


Lesson 20 Exercise
Matthew Walker
Balcar 3A 1/30/13

1. At any time after several TvShow objects have been instantiated, how would you find out
how many shows were instantiated? (Don’t use an object to do this.)

2. Would the code inside the numberOfShows method still be correct if numberOfShows
were non-static? If not, why?

3. Suppose the code inside the numberOfShows method is replaced with the following line:
return y;
Is this legal? If not, why?

4. Write code that will print the data member actor2. Do this without instantiating any
objects.

5. Is the following code legal? If not, why?
TvShow.setActor1(“Jimmy Stewart”);

6. Create an instance of TvShow called chrs (pass in the String “Cheers”) and use it to
access and print the class variable numShows.

7. Give the output of the following:
System.out.println(TvShow.x);
TvShow chrs = new TvShow(“Cheers”);
System.out.println(TvShow.x);
System.out.println(chrs.x);
TvShow hc = new TvShow(“History Channel”);
hc.x = 160;
System.out.println(TvShow.x);
System.out.println(hc.x);

8. Is the following a legal declaration of a class variable? If not, why?
static public char ch = ‘K’;

9. Write code that will cause the variable zxb to be a static state variable. The variable zxb is
a double.

10. Write code that will cause sn to be a constant static class member. The constant sn should
be initialized as an empty String.

11. What is the significance of the word Math when we use Math.pow(3.2, 4.001)?
Math is the class variable

12. Class variables are also called Static State variables.

13. Assuming that the appropriate static import has been done, rewrite the following code
without using the class name of the static methods.
double xop = Math.pow(Math.sqrt(x - zv), 3.1);


14. What are the two primary reasons for using the key-word, static?
            1. you can call a method without creating an object
2. they are shared by all objects

Programming Project code website

https://github.com/johnfonte/compsciap/tree/ff2f577800db4687276afdc71ca2ae43ee157eff

Tuesday, January 29, 2013

Gov WorkSheets

https://www.box.com/shared/v5xdq6qlmf
Mr. Sharpe worksheets

Thursday, January 24, 2013

lesson 19 excercise


Lesson 19 Exercise
Matthew walker
Balcar 3A 22/01/13
1. Write code that will create an array of 300 BankAccount objects. You are only to
instantiate two of them. The object with index 47 should have a beginning balance of
$92, and index 102 should have $1007. The name of your array will be ba.

BankAccount ba[]= new BankAccount[300];
ba[47] = new BankAccount ($92);
ba[102] = new BankAccount ($1002);

2. Write an if statement that will decide if k[3] is equal to jm[5] where it is assumed that k
and jm are numeric arrays.
If ( k[3] = = jm[5] )

3. Write an if statement that will decide if s[2] is equal to ss[19] where it is assumed that s
and ss are String arrays.
If (s[2].equals(ss[19]))

4. Write an if statement that will decide if cir[2] is equal to cirr[10] (with regard to content)
where it is assumed that cir and cirr are object arrays of type Circle.
If (cir[2].equals(cirr[10]))

5. What’s wrong with the following code?
char months[];
months[0] = ‘j’;
months has not been initialized yet

6. String suv[] = new String[20];
j = 0;
while(j < 17 )
    {
       suv[j] = “Hello”;
       j++;
    }
What is the logical size of the suv array? 20
What is the physical size of the suv array? 17

7. Write code using toCharArray to convert String d = “The quick brown fox jumped over
the lazy dog.” into the character array qbf.
char qbf[];
String d = "The quick brown fox jumped over the lazy dog.";
qbf = d.toCharArray();

8. double rub[] = {23.0, -102.1, 88.23, 111, 12.02, 189.119, 299.88};
double dub[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
Write a single line of code (using arraycopy) that will result in dub looking like this:
  1. {1, 2, 3, 4, 111, 12.02, 189.119, 8, 9}]

9. double[] zz, top = {12.1, 13.1, 14.1, 15.1, 18};
zz = top;
zz[2] = 99;
top[3] = 100.2;
Show what “both” arrays would look like at the completion of the above code.

10. char[] a, b;
a = “Groovy dude”.toCharArray( );
b = “I like this”.toCharArray( );
System.arraycopy(a, 1, b, 0, 4);
What do the two arrays look like at the completion of this code?

11. What must be true of any array before we can use Arrays.binarySearch( )?

12. Write code that will establish an array called myArray having the following elements,
{189.01, 2000, -32, 56, 182, 2}. Then sort the array.

13. Assume the array myArray in #12 has been correctly sorted. What would be printed with
the following?
System.out.println( Arrays.binarySearch(myArray, 56) );
System.out.println( Arrays.binarySearch(myArray, 102) );

14. What does the following print?
int xc[] = {123, 97, -102, 17};
int pk[] = {123, 79, -102, 17};
int gs[] = {123, 97, -102, 17};
System.out.println( Arrays.equals(xc, pk) + “\n” + Arrays.equals(xc, gs));
False
True

15. What does the following print?
int pickle[] = {1, 2, 3, 4, 5, 6, 7, 8};
Arrays.fill(pickle, -1);
System.out.println( pickle[4] );
-1

16. If a command line reads, java BigClass Munster Herman dude, what will the following
line inside the main method print?
System.out.println(“Name=” + args[2] +args[1] );

17. What’s printed by the following?
int px[] = {3, 4, 5, 6, 7, 8, 9};
System.out.println( px[ px[1] + 1 ]);
8

18. Write code using the “for-each” style of a for loop that will accumulate and print the
product of the state variables int jj within each object of object array objArray. Assume
the objects are created from the class DummyClass.