Lecture10

Lecture10 - Q1. Write the output on the line associated...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Q1. Write the output on the line associated with the code fragment. public class X { private int orange = 0; public void bar() { orange++; System.out.println( "B" + orange ); } } public class TestX { public static void main( String [] args ) { X x = new X(); System.out.println( "C" ); x.bar(); } }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Q2. What are the syntax errors from the following program? public class Enigma { public static void main(String[ ] args) { System.out.println("Input a String"); String x = scan.nextString( ); int size = x.length; char last = x.charAt(size-1); System.out.println("The last character in your string ", x, " is ", last); } } Answer: (1) the Scanner class has not been import so that the scan.nextString( ) will yield a syntax error. (2) The statement x.length requires parentheses as length is a method of the class String. (3) The System.out.println statement is not valid because of the use of “,” instead of “+” to concatenate the various parts of the String.
Background image of page 2
3 Using the Die class, write a method that will use two Die objects, roll them 100 times and return the number of times that a 7 or 11 is rolled on the two Die. //********************************************** // Die.java // Represents one die (singular of dice) with faces showing //values between 1 and 6. //********************************************** public class Die { private final int MAX = 6; // maximum face value private int faceValue; // current value showing on the die public Die() { faceValue = 1; } public int roll() { faceValue = (int)(Math.random() * MAX) + 1; return faceValue; }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 public void setFaceValue (int value) { faceValue = value; } public int getFaceValue() { return faceValue; } //-------------------------------------------------------------- // Returns a string representation of this die. //-------------------------------------------------------------- public String toString() { String result = Integer.toString(faceValue); return result; } } public int roll7or11( ) { Die d1 = new Die( ); Die d2 = new Die( ); int num = 0, sum = 0; for (int j = 0; j < 100; j++) { sum = d1.roll( ) + d2.roll( ); if (sum == 7|| sum == 11) num++; } return num; }
Background image of page 4
5 I have 2 classes in 2 different files. What is the output after we run java Test ? public class Test { public static void main(String[] args) { Holder h = new Holder(); h.held = 100; h.bump(h); System.out.println(h.held); } } public class Holder { public int held = 0; public void bump(Holder theHolder) { theHolder.held++; } } h
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 What will happen if we try to compile and run the following code ? public class Test {
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 22

Lecture10 - Q1. Write the output on the line associated...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online