int[] n = {3,4,5,6,7}; String str = Arrays.toString(n); System.out.println("n = " + str);
{4,3} vs {1,2,3,4,5} ==> {4,3} is larger since 4 > 1 {1,2,3,4} vs {1,3,5} ==> {1,3,5} is larger since 3 > 2 {1,2,3,4} vs {1,2,3} ==> {1,2,3,4} is larger since the first array is longer
MultiDisplay md = new MultiDisplay(); md.setDisplayMessage("Hello World!"); md.setDisplayCount(3); md.display(); // ==> print-out md.display("Goodbye cruel world!", 2); // ==> print-out System.out.println("Current Message: "+ md.getDisplayMessage());results in the following console print-out:
Hello World! Hello World! Hello World! Goodbye cruel world! Goodbye cruel world! Current Message: Goodbye cruel world!The class MultiDisplay should of course be able to handle other messages and other numbers of display counts.
System.out.println("Radio 1"); Radio r1 = new Radio(); System.out.println( r1.getSettings()); // Default settings // Update Radio 1 settings r1.turnOn(); r1.setVolume(3); r1.channelUp(); r1.channelUp(); r1.channelUp(); System.out.println( r1.getSettings()); // New settings r1.turnOff(); System.out.println( r1.getSettings()); // Reset default settings System.out.println("\nRadio 2"); Radio r2 = new Radio(); r2.volumeUp(); // Radio off ==> No adjustment possible r2.turnOn(); r2.volumeDown(); // volume = 0 ==> OK! r2.volumeDown(); // volume < 0 ==> error and neglect r2.setChannel(15); // out of range = ==> error and neglect System.out.println( r2.getSettings());results in the following console print-out:
Radio 1 Settings: On false, Channel 1, Volume 1 Settings: On true, Channel 4, Volume 3 Settings: On false, Channel 1, Volume 1 Radio 2 Radio off ==> No adjustment possible New volume not within range! New channel not within range! Settings: On true, Channel 1, Volume 0Notice
getSettings() // A string with current settings turnOn(), turnOff() // turnOff() ==> restore default settings setVolume(int newVolume), volumeUp(), volumeDown() // up/down ==> +- 1 step setChannel(int newChannel), channelUp(), channelDown() // up/down ==> +- 1 step
Point p1 = new Point(); Point p2 = new Point(3,4); System.out.println(p1.toString()); // ==> (0,0) System.out.println(p2.toString()); // ==> (3,4) if (p1.isEqualTo(p2)) // False! System.out.println("The two points are equal"); double dist = p1.distanceTo(p2); System.out.println("Point Distance: "+dist); p2.move(5,-2); // ==> (8,2) p1.moveToXY(8,2); // ==> (8,2) if (p1.isEqualTo(p2)) // True! System.out.println("The two points are equal");results in the following console print-out:
(0,0) (3,4) Point Distance: 5.0 The two points are equalThe class Point should of course be able to handle other points with different (x,y) values. Notice:
All classes are supposed to be commented and follow principles such as encapsulation.
Enter the size: 10 Enter the number of steps: 50 Enter the number of walks: 150 Out of 150 drunk people, 14 (9.34%) fell into the water.