2 Replies - 2392 Views - Last Post: 22 October 2007 - 09:12 PM Rate Topic: -----

#1 richiecomp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 15-October 06

C++ student needs help deciphering Java code

Posted 21 October 2007 - 09:51 PM

Hello, everyone!

I have been given the task of determining the validity of some code. Unfortunately, I received Java code by mistake, instead of C++. I was wondering if anyone out there could give me a hand with this one.

Analyze the following code:
public class Test1 {
  public Object max(Object o1, Object o2) {
	if ((Comparable)o1.compareTo(o2) >= 0) {
	  return o1;
	}
	else {
	  return o2;
	}
  }
}



Possible answers are:
a. The program has a syntax error because Test1 does not have a main method.
b. The program has a syntax error because o1 is an Object instance and it does not have the compareTo method.
c. The program has a syntax error because you cannot cast an Object instance o1 into Comparable.
d. The program would compile if ((Comparable)o1.compareTo(o2) >= 0)
is replaced by (((Comparable)o1).compareTo(o2) >= 0).
e. b and d are both correct.

--------------------------------------
Which of the following possible modifications will fix the errors in this code?
public class Test { 
  private double code;

  public double getCode() {
	return code;
  }

  protected abstract void setCode(double code);
}



Possible answers are:
a. Remove abstract in the setCode method declaration.
b. Change protected to public.
c. Add abstract in the class declaration.
d. b and c.

---------------------------------------------
Analyze the following code:
class Test { 
  public static void main(String[] args) {
	Object x = new Integer(2);
	System.out.println(x.toString());
  }
}



Possible answers are:
a. The program has syntax errors because an Integer object is assigned to x.
b. When x.toString() is invoked, the toString() method in the Object class is used.
c. When x.toString() is invoked, the toString() method in the Integer class is used.
d. None of the above.

-------------------------------------------------------------
Analyze the following code:
class Test { 
  public static void main(String[] args) {
	Object x = new Integer(2);
	System.out.println(x.doubleValue());
  }
}


Possible answers are:
a. The program has syntax errors because an Integer object is assigned to x.
b. The program has syntax errors because doubleValue() is not a method in the Object class.
c. The program compiles and runs fine.
d. None of the above.

-------------------------------------------
Analyze the following code:
class Test { 
  public static void main(String[] args) {
	Inner inner = new Inner();
	System.out.println(inner.k);
  }

  private class Inner {
	protected int k;
  }
}



Possible answers are:
a. The program has a syntax error because the Inner class does not have a constructor and you cannot create an object from it.
b. The program has a syntax error because the Inner class is private and it cannot be accessed in the main method .
c. The program has a syntax error because k is protected in the Inner class and it cannot be accessed in the main method.
d. The program has a syntax error because the Inner class is not static and it cannot be used to create an object in the main method.

--------------------------------------------------------
Analyze the following code:

Number numberRef = new Integer(0);	 
Double doubleRef = (Double)numberRef;



Possible answers:
a. You cannot assign an Integer object into a variable of the Number type.
b. The compiler detects that numberRef is not an instance of Double.
c. A runtime class casting exception occurs, since numberRef is not an instance of Double.
d. The program runs fine, since Integer is a subclass of Double.
e. You can convert an int to double, so you can cast an Integer instance to a Double instance.

-------------------------------------------------------------------
I have been manually trying to change the code, but wouldn't know where to begin. To top it all, I don't think the online java compiler is really trying its best to work properly.

Is This A Good Question/Topic? 0
  • +

Replies To: C++ student needs help deciphering Java code

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4333
  • View blog
  • Posts: 12,128
  • Joined: 18-April 07

Re: C++ student needs help deciphering Java code

Posted 21 October 2007 - 10:14 PM

I am sorry, perhaps you missed the rules. We don't help with what apparently looks like some kind of quiz or test. We can certainly help you (and already have) with problems you are working on (your own code) to find errors and correct them, but we can't help with this.

Sorry. :)
Was This Post Helpful? 0
  • +
  • -

#3 richiecomp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 15-October 06

Re: C++ student needs help deciphering Java code

Posted 22 October 2007 - 09:12 PM

Since I have no actual knowledge of Java, I turned to this forum for some needed help. Originally, I was supposed to get an assignment in C++, which is what I am studying. But, for some reason, I got sent these questions that were written in Java.

I did attempt an educated guess by going through every multiple choice answer, and doing exactly what it asked, waiting for an action. I did try to compile them in the online compiler located at:
http://www.zamples.c...?format=jdk16cl

On these questions, I kept getting a lot of errors on the online compiler, without actually telling me where the errors were located. And no suggestions, either.

As you can see from my previous thread, I was asked to produce an array with no duplicates. The course is in C++, but that assignment, somehow, was asked to be given in Java. (What the hell?!) I proceeded to do my job in C++, hoping to translate it into Java. For that, you guys came along and gave me tons of help, even producing a working code. I am eternally greatful.

The problem is, the mentor for my online course seems to have mixed the languages, and does not seem to realize the difference between Java and C++. The mentor has a doctorate in mathematics, but does not seem very knowledgeable about programming. We had a BIG disagreement over a test that was sent to me on which I answered questions that were right, but apparently wrong in the mentor's eyes. One of the questions was:
 Which of these data types requires the least amount of memory?
		a.	float
		b.	double
		c.	short
		d.	byte


I answered © short. The mentor marked me wrong, and stated that the correct answer is (d) byte. Not satisfied with that, I did some research and found that I was right. On C++, a short takes the least amout of memory, while in Java, a byte takes the least amount of memory. In essence, I have had to correct corrected tests because the mentor keeps mixing up the language. I was given credit, though.

Well, it has come to the point where I have given up trying to argue over the phone and through e-mail the situation over the correct assignments. If Java is what is given to me, then Java I shall return. Of course, there is one small problem: I read a whole book covering C++, but don't have the slightest idea how Java works.

I have been trying to decypher what has been given to me, with no luck. I am not sure what the proper heading for the code would be like, nor if I need a full blown IDE to make it work, as opposed to using an online Java compiler.

I would appreciate some guidance in this matter.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1