Solutions to the Try It Out Exercises in Java For Dummies, 7th Edition
by Barry Burd

Chapter 14: Sharing Names among the Parts of a Java Program

In this chapter:

Add getters and setters in SomeClass

class SomeClass { private int myField = 10; public int getMyField() { return myField; } public void setMyField(int myField) { this.myField = myField; } } class SomeOtherClass { public static void main(String args[]) { SomeClass someObject = new SomeClass(); System.out.println(someObject.getMyField()); } }

Look for the ShowFrame.java, Drawing.java, and ArtFrame.java files in your computer's File Explorer or Finder

Create a frame that displays a traffic signal with its green, yellow, and red lights

package com.burdbrain.drawings; import java.awt.Color; import java.awt.Graphics; public class Drawing { public int x = 40, y = 40, width = 40, height = 40; enum State { GREEN, YELLOW, RED } State state = State.GREEN; public void paint(Graphics g) { switch (state) { case GREEN: showGreen(g, false); showYellow(g, true); state = State.YELLOW; break; case YELLOW: showYellow(g, false); showRed(g, true); state = State.RED; break; case RED: showRed(g, false); showGreen(g, true); state = State.GREEN; break; } } void showGreen(Graphics g, boolean t) { g.setColor(t ? Color.GREEN : Color.WHITE); g.fillOval(x, y + 100, width, height); } void showYellow(Graphics g, boolean t) { g.setColor(t ? Color.YELLOW : Color.WHITE); g.fillOval(x, y + 50, width, height); } void showRed(Graphics g, boolean t) { g.setColor(t ? Color.RED : Color.WHITE); g.fillOval(x, y, width, height); } } package com.burdbrain.frames; import java.awt.Graphics; import javax.swing.JFrame; import com.burdbrain.drawings.Drawing; public class ArtFrame extends JFrame { private static final long serialVersionUID = 1L; Drawing drawing; public ArtFrame(Drawing drawing) { this.drawing = drawing; setTitle("Abstract Art"); setDefaultCloseOperation(EXIT_ON_CLOSE); } public void paint(Graphics g) { drawing.paint(g); } } import com.burdbrain.drawings.Drawing; import com.burdbrain.frames.ArtFrame; class ShowFrame { public static void main(String args[]) { ArtFrame artFrame = new ArtFrame(new Drawing()); artFrame.setSize(60, 200); artFrame.setVisible(true); while (true) { try { Thread.sleep(10000); artFrame.repaint(); } catch (InterruptedException e) { e.printStackTrace(); } } } }

Books and authors

public class Book { private String title; private Author author; public Book(String title, Author author) { this.title = title; this.author = author; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public Author getAuthor() { return author; } public void setAuthor(Author author) { this.author = author; } } import java.util.ArrayList; public class Author { private String name; private ArrayList<Book> books = new ArrayList<>(); public Author(String name) { this.name = name; } public void display() { System.out.println(name); for (Book book : books) { System.out.println(book.getTitle()); } } public String getName() { return name; } public void setName(String name) { this.name = name; } public ArrayList<Book> getBooks() { return books; } public void setBooks(ArrayList<Book> books) { this.books = books; } } public class Main { public static void main(String[] args) { Author dickens = new Author("Charles Dickens"); Book greatExpectations = new Book("Great Expectations", dickens); Book taleOfTwoCities = new Book("Tale of Two Cities", dickens); dickens.getBooks().add(greatExpectations); dickens.getBooks().add(taleOfTwoCities); dickens.display(); } }

Songs, albums, artists and playlists

public enum Genre { ROCK, POP, BLUES, CLASSICAL } import java.util.ArrayList; public class Artist { private String name; private ArrayList<Item> items = new ArrayList<>(); public Artist(String name) { super(); this.name = name; } public String getString() { return name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public ArrayList<Item> getItems() { return items; } public void add(Item item) { items.add(item); } } public class Item { private String name; private Artist artist; public Item(String name, Artist artist) { this.name = name; this.artist = artist; } public String getString() { return name + ", " + artist.getString() + "\n"; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Artist getArtist() { return artist; } public void setArtist(Artist artist) { this.artist = artist; } public String toString() { return (name + " " + artist.getName()); } } public class Song extends Item { private Genre genre; public Song(String name, Artist artist, Genre genre) { super(name, artist); this.genre = genre; } public String getString() { return getName() + ", " + getArtist().getString() + ", " + genre + "\n"; } public Genre getGenre() { return genre; } public void setGenre(Genre genre) { this.genre = genre; } @Override public String toString() { String string = super.toString() + " "; string += genre; return string; } } import java.util.ArrayList; public class Album extends Item { private ArrayList<Song> songs = new ArrayList<>(); public Album(String name, Artist artist) { super(name, artist); } public String getString() { String string = getName() + "\n"; for (Song song : songs) { string += " " + song.getString(); } return string; } public ArrayList<Song> getSongs() { return songs; } public void add(Song song) { songs.add(song); } @Override public String toString() { String string = super.toString() + " "; for (Song song : songs) { string += song.toString() + " "; } return string; } } import java.util.ArrayList; public class Playlist { private ArrayList<Item> items = new ArrayList<>(); public String getString() { String string = ""; for (Item item : items) { string += item.getString(); } return string; } public void addItem(Item item) { items.add(item); } public ArrayList<Item> getItems() { return items; } public void setItems(ArrayList<Item> items) { this.items = items; } } public class SongsAndAlbumsTest { public static void main(String[] args) { Artist john = new Artist("Elton John"); Item rocketMan = new Item("Rocket Man", john); Artist bach = new Artist("J.S. Bach"); Song wellTemperedClavier = new Song("Well Tempered Clavier", bach, Genre.CLASSICAL); bach.add(wellTemperedClavier); Song tocataAndFugue = new Song("Tocata and Fugue", bach, Genre.CLASSICAL); bach.add(tocataAndFugue); Artist joel = new Artist("Billy Joel"); Song movinOut = new Song("Movin' Out", joel, Genre.POP); joel.add(movinOut); Song justTheWayYouAre = new Song("Just the Way You Are", joel, Genre.POP); joel.add(justTheWayYouAre); Album stranger = new Album("The Stranger", joel); stranger.add(movinOut); stranger.add(justTheWayYouAre); joel.add(stranger); Playlist playlist = new Playlist(); playlist.addItem(stranger); playlist.addItem(rocketMan); playlist.addItem(wellTemperedClavier); playlist.addItem(tocataAndFugue); System.out.println(playlist.getString()); } }

Fix the code

package com.allmycode.things; import com.allyourcode.stuff.Stuff; import com.allyourcode.stuff.morestuff.MoreStuff; public class Things { protected int i = 0; protected int j = 0; int k = 0; public static void main(String[] args) { Stuff stuff = new Stuff(); System.out.println(stuff.i); MoreStuff moreStuff = new MoreStuff(); System.out.println(moreStuff.i); } } package com.allyourcode.stuff; import com.allyourcode.stuff.morestuff.MoreStuff; public class Stuff { public int i = 0; void aMethod() { new MoreStuff().myMethod(); } } package com.allyourcode.stuff.morestuff; import com.allmycode.things.Things; public class MoreStuff extends Things { public void myMethod() { System.out.println(i); } } package com.allmycode.things; public class MoreThings extends Things { public void anotherMethod() { System.out.println(i); System.out.println(j); System.out.println(k); } }