Skip to content

Instantly share code, notes, and snippets.

@mattcunningham
Last active January 18, 2017 18:03
Show Gist options
  • Save mattcunningham/7026dae095eac90a543397bab3ada531 to your computer and use it in GitHub Desktop.
Save mattcunningham/7026dae095eac90a543397bab3ada531 to your computer and use it in GitHub Desktop.
import java.util.List;
import java.util.ArrayList;
public class MasterSubscription {
private List<MagazineOrder> orders;
public MasterSubscription() {
orders = new ArrayList<MagazineOrder>();
}
public void addOrder(MagazineOrder theOrder) {
orders.add(theOrder);
}
public int getTotalMagazines() {
int total = 0;
for (MagazineOrder magazine : orders) {
total += magazine.getNumMagazines();
}
return total;
}
public int removeMagazine(String magazineName) {
int ret = 0;
for (int i = 0; i < orders.size(); i++) {
if (orders.get(i).getName().equals(magazineName)) {
ret += orders.get(i).getNumMagazines();
orders.remove(i);
i--;
}
}
return ret;
}
}
import java.util.Random;
public class Map {
private int[][] map;
private int rows;
private int cols;
public Map(int rows, int cols) {
this.rows = rows;
this.cols = cols;
}
public void generateMap() {
Random rand = new Random();
map = new int[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
map[i][j] = rand.nextInt(4) + 1;
}
}
}
public void showMap() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print(map[i][j] + " ");
}
System.out.println();
}
}
public int getElement(int row, int col) {
return map[row][col];
}
}
public class MusicSong {
private String songName;
private String songArtist;
private int songLength;
public MusicSong(String name, String artist, int length) {
this.songName = name;
this.songArtist = artist;
this.songLength = length;
}
public int getSongLength() {
return songLength;
}
}
import java.util.ArrayList;
public class MP3Player {
private ArrayList<MusicSong> mp3PlayerSongs;
public MP3Player() {
mp3PlayerSongs = new ArrayList<MusicSong>();
}
public void addSong(MusicSong newSong) {
mp3PlayerSongs.add(newSong);
}
public int totalSongTime() {
int total = 0;
for (MusicSong song : mp3PlayerSongs) {
total += song.getSongLength();
}
return total;
}
}
public class Rectangle {
private int height, width;
public Rectangle(int rectHeight, int rectWidth) {
this.height = rectHeight;
this.width = rectWidth;
}
public int getHeight() {
return height;
}
public int getWidth() {
return width;
}
public int getPerimeter() {
return (height * 2) + (width * 2);
}
public int getArea() {
return height * width;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment