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

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

In this chapter:

Everything's Better with Getters and Setters

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) { var someObject = new SomeClass(); System.out.println(someObject.getMyField()); } }

Now in Living Color

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; } case YELLOW -> { showYellow(g, false); showRed(g, true); state = State.RED; } case RED -> { showRed(g, false); showGreen(g, true); state = State.GREEN; } } } 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) { var 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(); } } } }

Buy This Book

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) { var dickens = new Author("Charles Dickens"); var greatExpectations = new Book("Great Expectations", dickens); var taleOfTwoCities = new Book("Tale of Two Cities", dickens); dickens.getBooks().add(greatExpectations); dickens.getBooks().add(taleOfTwoCities); dickens.display(); } }

Name That Tune

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) { var john = new Artist("Elton John"); Item rocketMan = new Item("Rocket Man", john); var bach = new Artist("J.S. Bach"); var wellTemperedClavier = new Song("Well Tempered Clavier", bach, Genre.CLASSICAL); bach.add(wellTemperedClavier); var tocataAndFugue = new Song("Tocata and Fugue", bach, Genre.CLASSICAL); bach.add(tocataAndFugue); var joel = new Artist("Billy Joel"); var movinOut = new Song("Movin' Out", joel, Genre.POP); joel.add(movinOut); var justTheWayYouAre = new Song("Just the Way You Are", joel, Genre.POP); joel.add(justTheWayYouAre); var stranger = new Album("The Stranger", joel); stranger.add(movinOut); stranger.add(justTheWayYouAre); joel.add(stranger); var playlist = new Playlist(); playlist.addItem(stranger); playlist.addItem(rocketMan); playlist.addItem(wellTemperedClavier); playlist.addItem(tocataAndFugue); System.out.println(playlist.getString()); } }

What's Mine is Yours

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