java klase
DESCRIPTION
jaTRANSCRIPT
PowerPoint Presentation
JAVA PRIMJERI-KLASE-
public class Song { private String title; private String author; public void setTitle(String title) { this.title = title; } public String getTitle() { return this.title; } public void setAuthor(String author) { this.author = author; } public String getAuthor() { return this.author; }
public class Main { public static void main(String[] args) {// System.out.println("That's All Right"); Song song = new Song(); song.setTitle("That's All Right"); System.out.println(song.getTitle());// song.author = "A. Crudup"; // System.out.println(song.title); }}
public class Album { private String title; private String author; private Song[] songs; public Album(String title, String author, Song[] songs) { this.title = title; this.author = author; this.songs = songs; } public Album() { } public void listSongs() { for (int i = 0; i < this.songs.length; i++) { System.out.println(songs[i].getTitle()); } }
public void setTitle(String title) { this.title = title; } public String getTitle() { return this.title; } public void setAuthor(String author) { this.author = author; } public String getAuthor() { return this.author; } public void setSongs(Song[] songs) { this.songs = songs; } public Song[] getSongs() { return this.songs; }
public class Main { public static void main(String[] args) {// System.out.println("That's All Right");// Song song = new Song();// // song.setTitle("That's All Right");// System.out.println(song.getTitle());// song.author = "A. Crudup"; // System.out.println(song.title);
Test t = new Test(); // t.testSettersAndGetters();// t.testConstructors(); t.testArrays(); }}q
public class Album { private String title; private String author; private Song[] songs; public Album(String title, String author, Song[] songs) { this.title = title; this.author = author; this.songs = songs; } public Album() { } public void listSongs() { for (int i = 0; i < this.songs.length; i++) { System.out.println(songs[i].getTitle()); } }
public void listSongs(boolean loopType) { int i = 0; if (loopType == true) { while (i < this.songs.length) {// if (i == 1) {// i++;// continue;// } if (i == 2) { break; } System.out.println(songs[i].getTitle()); i++; // i = i + 1; } } else { do { System.out.println(songs[i].getTitle()); i++; } while (i < this.songs.length);// System.out.println("Whatever"); } }
public class Main { public static void main(String[] args) {// System.out.println("That's All Right");// Song song = new Song();// // song.setTitle("That's All Right");// System.out.println(song.getTitle());// song.author = "A. Crudup"; // System.out.println(song.title);
Test t = new Test(); // t.testSettersAndGetters();// t.testConstructors();// t.testArrays();// t.testLoops();// t.testScanner(); t.testNumberFormat(); }}
PRIMJER NASLEDJIVANJA I VEZIVANAJ VISE KLASA: album, song, rocksong, southernrocksong,test,main
public class Album { private String title; private String author; private Song[] songs; public Album(String title, String author, Song[] songs) { this.title = title; this.author = author; this.songs = songs; } public Album() { } public void listSongs() { for (int i = 0; i < this.songs.length; i++) { System.out.println(songs[i].getTitle()); } }
public void listSongs(boolean loopType) { int i = 0; if (loopType == true) { while (i < this.songs.length) {// if (i == 1) {// i++;// continue;// } if (i == 2) { break; } System.out.println(songs[i].getTitle()); i++; // i = i + 1; } } else { do { System.out.println(songs[i].getTitle()); i++; } while (i < this.songs.length);// System.out.println("Whatever"); } }
public void setTitle(String title) { this.title = title; } public String getTitle() { return this.title; } public void setAuthor(String author) { this.author = author; } public String getAuthor() { return this.author; } public void setSongs(Song[] songs) { this.songs = songs; } public Song[] getSongs() { return this.songs; }}
public class Song { private static int year; protected String title; protected String author; static { year = 1955; } public Song(String title, String author) { this.title = title; this.author = author; } public Song() { } public static void showDefinition() { System.out.println("In music, a song is a composition for voice performed by singing or alongside musical instruments."); }
public String toString() { return this.title + ", by " + this.author; } public void setTitle(String title) { this.title = title; } public String getTitle() { return this.title; } public void setAuthor(String author) { this.author = author; } public String getAuthor() { return this.author; } public void setYear(int year) { this.year = year; } public int getYear() { return this.year; }
public class RockSong extends Song {
public static final String DEFINING_FEATURE = "Electric guitar";private String meaning;
public RockSong() {super();// TODO Auto-generated constructor stub}
public RockSong(String title, String author) {super(title, author);// TODO Auto-generated constructor stub}
}
public RockSong(String title, String author, String meaning) {super(title, author);this.meaning = meaning;}
public String toString() { return this.title + ", by " + this.author + ", a nice Rock song"; } public String getMeaning() {return meaning;}
public void setMeaning(String meaning) {this.meaning = meaning;}
}
public class SouthernRockSong extends RockSong {
private int noOfLeadGuitars;
public SouthernRockSong() {super();// TODO Auto-generated constructor stub}
public SouthernRockSong(String title, String author, String meaning) {super(title, author, meaning);// TODO Auto-generated constructor stub}
public SouthernRockSong(String title, String author, String meaning, int noOfLeadGuitars) {super(title, author, meaning);this.noOfLeadGuitars = noOfLeadGuitars;// TODO Auto-generated constructor stub}
public SouthernRockSong(String title, String author) {super(title, author);// TODO Auto-generated constructor stub}
public SouthernRockSong(int noOfLeadGuitars) {super();this.noOfLeadGuitars = noOfLeadGuitars;}
public String toString() { return this.title + ", by " + this.author + ", a nice Rock song" + " from the South"; } public int getNoOfLeadGuitars() {return noOfLeadGuitars;}
public void setNoOfLeadGuitars(int noOfLeadGuitars) {this.noOfLeadGuitars = noOfLeadGuitars;}
}
package test;import java.util.Scanner;import java.text.NumberFormat;
import music.Album;import music.RockSong;import music.Song;import music.SouthernRockSong;
public class Test { public static final Scanner IN = new Scanner(System.in); public void testSettersAndGetters() { Song song = new Song(); song.setTitle("That's All Right"); System.out.println(song.getTitle());
} public void testConstructors() { Song song = new Song("That's All Right", "Elvis Presley");// song.setTitle("That's All Right"); System.out.println(song.getTitle()); }
public void testArrays() { Song gloryDays = new Song("Glory Days", "Bruce Springsteen"); Song dancingInTheDark = new Song("Dancing in the Dark", "Bruce Springsteen"); Song hungryHeart = new Song("Hungry Heart", "Bruce Springsteen"); Song[] songs = new Song[3]; songs[0] = gloryDays; songs[1] = dancingInTheDark; songs[2] = hungryHeart; Album bornInTheUSA = new Album("Born in the USA", "Bruce Springsteen", songs); bornInTheUSA.listSongs(); } public void testLoops() { Song gloryDays = new Song("Glory Days", "Bruce Springsteen"); Song dancingInTheDark = new Song("Dancing in the Dark", "Bruce Springsteen"); Song hungryHeart = new Song("Hungry Heart", "Bruce Springsteen"); Song[] songs = new Song[3]; songs[0] = gloryDays; songs[1] = dancingInTheDark; songs[2] = hungryHeart; Album bornInTheUSA = new Album("Born in the USA", "Bruce Springsteen", songs); bornInTheUSA.listSongs(true);// bornInTheUSA.listSongs(false); }
public void testScanner() { Song song = new Song(); System.out.print("Enter the song title: "); String title = IN.nextLine(); song.setTitle(title); System.out.println(song.getTitle()); } public void testNumberFormat() { double a = 45.234; double b = 33.67; System.out.println(a/b); NumberFormat nf = NumberFormat.getInstance(); nf.setMaximumFractionDigits(2); System.out.println(nf.format(a/b)); } public void testStatic() {// Song.showDefinition(); Song song = new Song(); song.showDefinition(); song.setTitle("That's All Right"); song.setYear(1954); System.out.println("That's All Right: " + song.getYear()); Song johnnyBGood = new Song("Johnny B. Good", "Chuck Berry"); System.out.println("Johnny B. Good: " + johnnyBGood.getYear()); }
public void testStrings() {// String s = "Arthur Crudup"; String s = new String("Arthur Crudup"); char c[] = {'D', 'r', 'e', 'a', 'm', 's'}; String song = new String(c); System.out.println(song); String aString = song.concat(s); System.out.println(aString); System.out.println(aString.toUpperCase()); StringBuffer song1 = new StringBuffer(); song1.append(song); System.out.println(song1); song1.append(" are so nice things that it is beyond any imagination."); System.out.println(song1); song1.delete(13, 34); System.out.println(song1);// StringBuffer sb = "Arthur Crudup"; }
public void testToString() { Song gloryDays = new Song("Glory Days", "Bruce Springsteen");// System.out.println(gloryDays.getAuthor()); System.out.println(gloryDays); } public void testInheritance() { Song thatsAllRight = new Song("That's All Right", "Elvis Presley"); RockSong purpleHaze = new RockSong("Purple Haze", "Jimi Hendrix", "reflexive"); SouthernRockSong whippingPost = new SouthernRockSong("Whipping Post", "Allman Brothers Band", "humiliation", 2); Song[] songs = new Song[3]; songs[0] = thatsAllRight; songs[1] = purpleHaze; songs[2] = whippingPost; for (int i = 0; i < songs.length; i++) {System.out.println(songs[i]);} }}
package main;
import test.Test;
public class Main { public static void main(String[] args) {// System.out.println("That's All Right");// Song song = new Song();// // song.setTitle("That's All Right");// System.out.println(song.getTitle());// song.author = "A. Crudup"; // System.out.println(song.title);
Test t = new Test(); // t.testSettersAndGetters();// t.testConstructors();// t.testArrays();// t.testLoops();// t.testScanner();// t.testNumberFormat();// t.testStatic();// t.testStrings();// t.testToString(); t.testInheritance(); }}
package music;
import java.util.ArrayList;
public class PlayList {
private ArrayList songs;
public PlayList(ArrayList songs) {super();this.songs = songs;}
public PlayList() {super();songs = new ArrayList();}
public void addSong(Song song) {songs.add(song);}
public void removeSong(int i) {songs.remove(i);}
public void showPlayList() {System.out.println("Playlist:");for (int i = 0; i < songs.size(); i++) {System.out.println(songs.get(i));}}
public ArrayList getSongs() {return songs;}
public void setSongs(ArrayList songs) {this.songs = songs;}
}
package music;
public class Album { private String title; private String author; private Song[] songs; public Album(String title, String author, Song[] songs) { this.title = title; this.author = author; this.songs = songs; } public Album() { } public void listSongs() { for (int i = 0; i < this.songs.length; i++) { System.out.println(songs[i].getTitle()); } }
public void listSongs(boolean loopType) { int i = 0; if (loopType == true) { while (i < this.songs.length) {// if (i == 1) {// i++;// continue;// } if (i == 2) { break; } System.out.println(songs[i].getTitle()); i++; // i = i + 1; } } else { do { System.out.println(songs[i].getTitle()); i++; } while (i < this.songs.length);// System.out.println("Whatever"); } }
public void setTitle(String title) { this.title = title; } public String getTitle() { return this.title; } public void setAuthor(String author) { this.author = author; } public String getAuthor() { return this.author; } public void setSongs(Song[] songs) { this.songs = songs; } public Song[] getSongs() { return this.songs; }}
package music;
public class EmptySetListException extends Exception {
public EmptySetListException() {super();// TODO Auto-generated constructor stub}
public EmptySetListException(String arg0, Throwable arg1, boolean arg2,boolean arg3) {super(arg0, arg1, arg2, arg3);// TODO Auto-generated constructor stub}
public EmptySetListException(String arg0, Throwable arg1) {super(arg0, arg1);// TODO Auto-generated constructor stub}
public EmptySetListException(String arg0) {super(arg0);// TODO Auto-generated constructor stub}
public EmptySetListException(Throwable arg0) {super(arg0);// TODO Auto-generated constructor stub}
package music;
import java.util.ArrayList;
public class PlayList implements SongList {
private ArrayList songs;
public PlayList(ArrayList songs) {super();this.songs = songs;}
public PlayList() {super();songs = new ArrayList();}
public void addSong(Song song) {songs.add(song);}
public void removeSong(int i) {songs.remove(i);}
public void showPlayList() {System.out.println("Playlist:");for (int i = 0; i < songs.size(); i++) {System.out.println(songs.get(i));}}
public ArrayList getSongs() {return songs;}
public void setSongs(ArrayList songs) {this.songs = songs;}
}
package music;
public class Song { private static int year; protected String title; protected String author; static { year = 1955; } public Song(String title, String author) { this.title = title; this.author = author; } public Song() { } public static void showDefinition() { System.out.println("In music, a song is a composition for voice performed by singing or alongside musical instruments."); }
public String toString() { return this.title + ", by " + this.author; } // public boolean equals(Object obj) {// if (obj instanceof Song) {// Song s = (Song) obj;// if (this.title.equals(s.getTitle()) && this.author.equals(s.getAuthor())) {// return true;// } else {// return false;// }// }// return false;// } @Overridepublic int hashCode() {final int prime = 31;int result = 1;result = prime * result + ((author == null) ? 0 : author.hashCode());result = prime * result + ((title == null) ? 0 : title.hashCode());return result;}
@Overridepublic boolean equals(Object obj) {if (this == obj)return true;if (obj == null)return false;if (getClass() != obj.getClass())return false;Song other = (Song) obj;if (author == null) {if (other.author != null)return false;} else if (!author.equals(other.author))return false;if (title == null) {if (other.title != null)return false;} else if (!title.equals(other.title))return false;return true;}
public void setTitle(String title) { this.title = title; } public String getTitle() { return this.title; } public void setAuthor(String author) { this.author = author; } public String getAuthor() { return this.author; } public void setYear(int year) { this.year = year; } public int getYear() { return this.year; } }
package music;
public class RockSong extends Song {
public static final String DEFINING_FEATURE = "Electric guitar";private String meaning;
public RockSong() {super();// TODO Auto-generated constructor stub}
public RockSong(String title, String author) {super(title, author);// TODO Auto-generated constructor stub}
public RockSong(String title, String author, String meaning) {super(title, author);this.meaning = meaning;}
public String toString() { return this.title + ", by " + this.author + ", a nice Rock song"; } public String getMeaning() {return meaning;}
public void setMeaning(String meaning) {this.meaning = meaning;}
}
package music;
import java.util.ArrayList;
public class SetList implements SongList {
private ArrayList songs;
public SetList(ArrayList songs) {super();this.songs = songs;}
public SetList() {super();this.songs = new ArrayList();}
@Overridepublic void addSong(Song song) {songs.add(song.getTitle());}
@Overridepublic void removeSong(int i) {songs.remove(i);}
public void showSetList() {System.out.println("Setlist:");for (int i = 0; i < songs.size(); i++) {System.out.println(songs.get(i));}}
public ArrayList getSongs() {return songs;}
public void setSongs(ArrayList songs) {this.songs = songs;}
}
package music;
public interface SongList {
public void addSong(Song song);public void removeSong(int i);
}
package music;
public class SouthernRockSong extends RockSong {
private int noOfLeadGuitars;
public SouthernRockSong() {super();// TODO Auto-generated constructor stub}
public SouthernRockSong(String title, String author, String meaning) {super(title, author, meaning);// TODO Auto-generated constructor stub}
public SouthernRockSong(String title, String author, String meaning, int noOfLeadGuitars) {super(title, author, meaning);this.noOfLeadGuitars = noOfLeadGuitars;// TODO Auto-generated constructor stub}
public SouthernRockSong(String title, String author) {super(title, author);// TODO Auto-generated constructor stub}
public SouthernRockSong(int noOfLeadGuitars) {super();this.noOfLeadGuitars = noOfLeadGuitars;}
public String toString() { return this.title + ", by " + this.author + ", a nice Rock song" + " from the South"; } public int getNoOfLeadGuitars() {return noOfLeadGuitars;}
public void setNoOfLeadGuitars(int noOfLeadGuitars) {this.noOfLeadGuitars = noOfLeadGuitars;}
}
package test;import java.util.Scanner;import java.text.NumberFormat;
import music.Album;import music.PlayList;import music.RockSong;import music.SetList;import music.Song;import music.SongList;import music.SouthernRockSong;
public class Test { public static final Scanner IN = new Scanner(System.in); public void testSettersAndGetters() { Song song = new Song(); song.setTitle("That's All Right"); System.out.println(song.getTitle());
}
public void testConstructors() { Song song = new Song("That's All Right", "Elvis Presley");// song.setTitle("That's All Right"); System.out.println(song.getTitle()); } public void testArrays() { Song gloryDays = new Song("Glory Days", "Bruce Springsteen"); Song dancingInTheDark = new Song("Dancing in the Dark", "Bruce Springsteen"); Song hungryHeart = new Song("Hungry Heart", "Bruce Springsteen"); Song[] songs = new Song[3]; songs[0] = gloryDays; songs[1] = dancingInTheDark; songs[2] = hungryHeart; Album bornInTheUSA = new Album("Born in the USA", "Bruce Springsteen", songs); bornInTheUSA.listSongs(); }
public void testLoops() { Song gloryDays = new Song("Glory Days", "Bruce Springsteen"); Song dancingInTheDark = new Song("Dancing in the Dark", "Bruce Springsteen"); Song hungryHeart = new Song("Hungry Heart", "Bruce Springsteen"); Song[] songs = new Song[3]; songs[0] = gloryDays; songs[1] = dancingInTheDark; songs[2] = hungryHeart; Album bornInTheUSA = new Album("Born in the USA", "Bruce Springsteen", songs); bornInTheUSA.listSongs(true);// bornInTheUSA.listSongs(false); } public void testScanner() { Song song = new Song(); System.out.print("Enter the song title: "); String title = IN.nextLine(); song.setTitle(title); System.out.println(song.getTitle()); }
public void testNumberFormat() { double a = 45.234; double b = 33.67; System.out.println(a/b); NumberFormat nf = NumberFormat.getInstance(); nf.setMaximumFractionDigits(2); System.out.println(nf.format(a/b)); } public void testStatic() {// Song.showDefinition(); Song song = new Song(); song.showDefinition(); song.setTitle("That's All Right"); song.setYear(1954); System.out.println("That's All Right: " + song.getYear()); Song johnnyBGood = new Song("Johnny B. Good", "Chuck Berry"); System.out.println("Johnny B. Good: " + johnnyBGood.getYear()); }
public void testStrings() {// String s = "Arthur Crudup"; String s = new String("Arthur Crudup"); char c[] = {'D', 'r', 'e', 'a', 'm', 's'}; String song = new String(c); System.out.println(song); String aString = song.concat(s); System.out.println(aString); System.out.println(aString.toUpperCase()); StringBuffer song1 = new StringBuffer(); song1.append(song); System.out.println(song1); song1.append(" are so nice things that it is beyond any imagination."); System.out.println(song1); song1.delete(13, 34); System.out.println(song1);// StringBuffer sb = "Arthur Crudup"; }
public void testToString() { Song gloryDays = new Song("Glory Days", "Bruce Springsteen");// System.out.println(gloryDays.getAuthor()); System.out.println(gloryDays); } public void testInheritance() { Song thatsAllRight = new Song("That's All Right", "Elvis Presley"); RockSong purpleHaze = new RockSong("Purple Haze", "Jimi Hendrix", "reflexive"); SouthernRockSong whippingPost = new SouthernRockSong("Whipping Post", "Allman Brothers Band", "humiliation", 2); Song[] songs = new Song[3]; songs[0] = thatsAllRight; songs[1] = purpleHaze; songs[2] = whippingPost; for (int i = 0; i < songs.length; i++) {System.out.println(songs[i]);} // purpleHaze = thatsAllRight; }
public void testEquals() { Song thatsAllRight = new Song("That's All Right", "Elvis Presley"); Song thatsAllRight1 = new Song("That's All Right", "Elvis Presley"); // if (thatsAllRight == thatsAllRight1) {// System.out.println("They are the same!?!?");// } else {// System.out.println("No way.");// } if (thatsAllRight.equals(thatsAllRight1)) { System.out.println("They are the same!"); } else { System.out.println("No way."); } System.out.println(); String s1 = thatsAllRight.getTitle(); String s2 = thatsAllRight.getTitle(); if (s1 == s2) { System.out.println("Hey, they are equal !!!"); } else { System.out.println("They are different."); } }
public void testLists() { PlayList playlist = new PlayList(); Song thatsAllRight = new Song("That's All Right", "Elvis Presley"); RockSong purpleHaze = new RockSong("Purple Haze", "Jimi Hendrix", "reflexive"); SouthernRockSong whippingPost = new SouthernRockSong("Whipping Post", "Allman Brothers Band", "humiliation", 2); playlist.addSong(thatsAllRight); playlist.addSong(purpleHaze); playlist.addSong(whippingPost); playlist.showPlayList(); System.out.println(); playlist.removeSong(1); playlist.showPlayList(); }
public void testInterfaces() {// PlayList playlist = new PlayList(); SongList playlist = new PlayList(); SongList setlist = new SetList(); Song thatsAllRight = new Song("That's All Right", "Elvis Presley"); RockSong purpleHaze = new RockSong("Purple Haze", "Jimi Hendrix", "reflexive"); SouthernRockSong whippingPost = new SouthernRockSong("Whipping Post", "Allman Brothers Band", "humiliation", 2); playlist.addSong(thatsAllRight);// playlist.addSong(new Song("That's All Right", "Elvis Presley")); playlist.addSong(purpleHaze); playlist.addSong(whippingPost); setlist.addSong(thatsAllRight); setlist.addSong(purpleHaze); setlist.addSong(whippingPost); PlayList pl = (PlayList) playlist; pl.showPlayList(); // ((PlayList) playlist).showPlayList(); System.out.println(); ((SetList) setlist).showSetList(); }
public void testExceptions() { int [] a = new int[3]; for (int i = 0; i