Variables and Java vs C 1 What can
Variables and Java vs C++ 1
What can be improved? (variables) public void go. Direction(String direction. Name) { boolean went. To. Room = false; for (Direction direction : current. Room. get. Directions()) { if (direction. get. Direction. Name(). equals. Ignore. Case(direction. Name)) { went. To. Room = true; current. Room = direction. get. Destination. Room(); break; } } if (!went. To. Room) { System. out. println("I can't go " + direction. Name); } } A) Eliminating temporary variable B) Eliminating intermediate results C) Eliminating control flow variable D) Shrinking scope of variable E) Prefer write once variable 2
What can be improved? (variables) public static void main(String[] args) { String curr. Room. Name = ""; // deals with args. . . Layout map. Layout = User. Interface. Load. Map(new. Map. Url); Map<String, Room> play. Map = Game. State. Generate. Virtual. Map(map. Layout); Room curr. Room = play. Map. get(map. Layout. get. Starting. Room()); while (continue. Playing) { curr. Room. Name = Game. State. play(curr. Room); curr. Room = play. Map. get(curr. Room. Name); if (curr. Room. Name. equals("EXIT")) { continue. Playing = false; } } } A) Eliminating temporary variable B) Eliminating intermediate results C) Eliminating control flow variable D) Shrinking scope of variable E) Prefer write once variable 3
What can be improved? A) Eliminating temporary variable B) Eliminating intermediate results C) Eliminating control flow variable D) Shrinking scope of variable E) Prefer write once variable String description; String current. Room = layout. get. Starting. Room(); String done = ""; for (int i = 0; i < layout. get. Rooms(). length; ) { int current. Room. Index = layout. get. Room. From. Name(current. Room); description = layout. get. Rooms()[current. Room. Index]. get. Description(); System. out. println(description); Array. List<String> direction. Name = new Array. List<String>(); for (int j = 0; j < layout. get. Rooms()[current. Room. Index]. get. Directions(). length; j++) { direction. Name. add(layout. get. Rooms()[current. Room. Index]. get. Directions()[j]. get. Direction. Name(). to. Lower. Case()); } String direction = get. Directions. Option(direction. Name); 4
What can be improved? (variables) public static void check. Floor. Plan() throws Exception { //. . . (removed stuff) for (Room curr. Room : room. Collection. values()) { boolean room. Found = false; for (Direction curr. Direction : curr. Room. get. Directions()) { room. Found = room. Found || find. Room. In. Connecting(curr. Room. get. Name(), room. Collection. get(curr. Direction. get. Room())); } if (!room. Found) { throw new Bad. Floor. Plan. Json. Exception("Rooms not connected. "); } } } A) Eliminating temporary variable B) Eliminating intermediate results C) Eliminating control flow variable D) Shrinking scope of variable E) Prefer write once variable 5
Which is better? A String original. Direction. Name = input. substring(3); return "I can't go " + original. Direction. Name + "n"; B return "I can't go " + input. substring(3) + "n"; 6
Which is better? A String input = scanner. next. Line(); String output = game. Controller. handle. Input(input); B String output = game. Controller. handle. Input(scanner. next. Line()); 7
Java and C++ ¢ ¢ Similar in: § Syntax: Java was designed to use syntax similar to C++ to ease adoption § Structurally: Both are object-oriented languages Different in goals: § Java designed for: safety and portability § C++ designed for: Control and performance 8
How much time did Adventure assignment take? A. B. C. D. E. 0 – 3 hours 4 – 6 hours 7 – 9 hours 10– 12 hours More than 12 hours 9
How much difficult was the Adventure assignment? A. B. C. D. E. Trivial Easy Reasonable Difficult Excessive 10
Java vs C++ Memory Java ¢ Automatic § Allocated on stack § Lifetime from function ¢ Garbage Collected § All arrays § All Classes § Allocated on the heap § Lifetime as long as C++ ¢ Automatic § Allocated on stack § Lifetime from function ¢ Heap § Allocated on the heap § Lifetime managed by various methods referenced 11
Java vs C++ Functions ¢ ¢ Java § All functions belong to a class (methods) § All arguments call by value § static used to allow methods to be called without an object. C++ § Functions just exist § Classes can have functions just like in Java called methods they then get a implicit this argument 12
Java Array vs C++ Array Java ¢ ¢ ¢ Allocated on heap Checks bounds Think of as an object Size set when created Knows length C++ ¢ ¢ Allocated either on heap or stack No bounds check on accesses Think of as a pointer Size set when allocated 13
Java Array vs C++ Vector Java Array ¢ ¢ ¢ Allocated on heap Checks bounds Think of as an object Size set when created Knows length C++ std: : vector ¢ ¢ Allocated on heap Can check bounds Can extend Knows length 14
- Slides: 14