Last active
September 27, 2015 05:07
-
-
Save twinkfrag/5a16342c384d4c0dfbc1 to your computer and use it in GitHub Desktop.
アルゴ演習15-1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by @twinkfrag on 2015/09/26. | |
*/ | |
import java.util.*; | |
import java.util.stream.*; | |
/** | |
* アルゴ演習15の課題に関して、解説とJava8などでの改良点 | |
* 問題に対する解答として間違っている可能性もあるので注意 | |
* <p> | |
* 1週目 | |
*/ | |
public class Algo15_ex1 { | |
public static void main(String[] args) { | |
} | |
} | |
/** | |
* 以下、提出したクラス | |
*/ | |
class IntArrayTest { | |
public static void main(String[] args) { | |
// try-with-resource statement | |
// tryの括弧内でautoClosableなリソースを作ると | |
// ブロックから抜けるときに(開かれている場合)closeさせる。 | |
// この場合、catch, finallyは書いても書かなくてもよい | |
try (Scanner scanner = new Scanner(System.in)) { | |
System.out.println("配列の大きさを入れてください。"); | |
// tryのネストを減らしてSystem.exit()を回避 | |
// 無意味なnull代入を減らすため | |
// System.exit()はその後のコードを通らない判定をしてくれないので | |
// numbers宣言時にnullでも代入しないと「代入されていない」エラーが出るが、 | |
// 実際にはnull状態ではエラー箇所は通らない上に通ってもNullPointerなので無駄 | |
// Exceptionが出た後に通らなくてよい処理はtry-catchの中でいいと思う | |
// 配列の宣言と確保 | |
// 数値以外の場合はScanner#nextInt()からInputMismatchExceptionが | |
// 負数を入力した場合はnew int[]からNegativeArraySizeExceptionが吐かれる | |
int[] numbers = new int[scanner.nextInt()]; | |
if (numbers.length < 1) { | |
// 配列長が0なら負と同じ処理 | |
throw new NegativeArraySizeException(); | |
} | |
System.out.println("整数値を" + numbers.length + "個入れてください。"); | |
// 配列に値を入力 | |
// 本当はfor文なんか使いたくないが[]演算子は左辺値のときのみポインタになるため | |
// for(int i : numbers)などでは回せない(数値のコピーに代入することになる) | |
for (int i = 0; i < numbers.length; i++) { | |
numbers[i] = scanner.nextInt(); | |
} | |
// 入力された値を表示 | |
System.out.println("入力された値は"); | |
// 配列を整形してtoString | |
System.out.println(Arrays.toString(numbers)); | |
// streamAPIは一度終端させると使い回せないので毎回生成する必要がある | |
// 合計の表示 | |
// IntStreamのsum()の戻り値はint | |
System.out.println("合計は" + Arrays.stream(numbers).sum()); | |
// 平均の表示 | |
// average()の戻り値はOptionalDouble | |
// OptionalがEmptyの場合にはDouble.NaNを返す | |
System.out.println("平均は" + Arrays.stream(numbers).average().orElse(Double.NaN)); | |
} catch (InputMismatchException e) { | |
System.out.println("数値の形式が違います。"); | |
e.printStackTrace(); | |
} catch (NegativeArraySizeException e) { | |
System.out.println("ゼロより大きな値を入れてください。"); | |
} | |
} | |
// 13年につくったやつ(Java6) | |
public static class IntArrayTest13 { | |
public static void main(String[] args) { | |
Scanner scanner = new Scanner(System.in); | |
int[] numbers = null; // 整数型の配列を入れる変数宣言 | |
System.out.println("配列の大きさを入れてください。"); | |
try { | |
int n = scanner.nextInt(); // 個数の入力 | |
if (n <= 0) { | |
System.out.println("ゼロより大きな値を入れてください。"); | |
System.exit(0); | |
} | |
// 配列の宣言と確保 | |
numbers = new int[n]; | |
System.out.println("整数値を" + n + "個入れてください。"); | |
// 配列に値を入力 | |
for (int i = 0; i < numbers.length; i++) { | |
numbers[i] = scanner.nextInt(); | |
} | |
} catch (InputMismatchException e) { | |
System.out.println("数値の形式が違います。"); | |
e.printStackTrace(); | |
System.exit(0); | |
} | |
// 入力された値を表示 | |
// ついでに合計をつくる | |
System.out.println("入力された値は"); | |
int sum = 0; | |
for (int i = 0; i < numbers.length; i++) { | |
System.out.print(numbers[i] + ","); | |
sum += numbers[i]; | |
} | |
System.out.println(); | |
// 合計の表示 | |
System.out.println("合計は" + sum); | |
// 平均の表示 | |
System.out.println("平均は" + (float) sum / numbers.length); | |
} | |
} | |
} | |
class IntArrayListTest { | |
public static void main(String[] args) { | |
// try-with-resource statement | |
try (Scanner scanner = new Scanner(System.in)) { | |
// 整数を要素とするArrayListの宣言と生成 | |
// 両辺にジェネリックがある場合、右辺のジェネリックを省略(Java7) | |
ArrayList<Integer> intList = new ArrayList<>(); | |
try { | |
System.out.println("整数値を入力してください。整数以外を入力するまで繰り返します。"); | |
// 入力部分 | |
// Integer.TryParseがあればtry-catchで入力終了判定しなくてもいいんだが… | |
while (true) { | |
intList.add(scanner.nextInt()); | |
} | |
} catch (InputMismatchException e) { | |
System.out.println("入力が完了しました。データの数は" + intList.size()); | |
} | |
// 入力された値を表示 | |
System.out.println("入力された値は"); | |
// 配列を整形してtoString(書かなくても暗黙的に呼ばれるが) | |
System.out.println(intList.toString()); | |
// sum()メソッドがあるのはIntStreamでStream<Integer>ではないので前者を生成 | |
// mapToIntの引数でAutoUnboxing | |
// 合計の表示 | |
// IntStreamのsum()の戻り値はint | |
System.out.println("合計は" + intList.stream().mapToInt(x -> x).sum()); | |
// 平均の表示 | |
// average()の戻り値はOptionalDouble | |
System.out.println("平均は" + intList.stream().mapToInt(x -> x).average().orElse(Double.NaN)); | |
} | |
} | |
/** | |
* 13年につくったやつ (Java6) | |
*/ | |
public static class IntArrayListTest12 { | |
public static void main(String[] args) { | |
Scanner scanner = new Scanner(System.in); | |
// 整数を要素とするArrayListの宣言と生成 | |
ArrayList<Integer> intList = new ArrayList<Integer>(); | |
try { | |
System.out.println("整数値を入力してください。整数以外を入力するまで繰り返します。"); | |
// 入力部分 | |
while (true) { | |
intList.add(scanner.nextInt()); | |
} | |
} catch (InputMismatchException e) { | |
System.out.println("入力が完了しました。データの数は" + intList.size()); | |
} | |
int sum = 0; | |
// 入力された値を表示する | |
System.out.println("入力された値は"); | |
for (int i : intList) { | |
System.out.print(i + ", "); | |
sum += i; | |
} | |
System.out.println(); | |
// 合計を表示する | |
System.out.println("合計は" + sum); | |
// 平均を表示する | |
System.out.println("平均は" + (float) sum / intList.size()); | |
} | |
} | |
} | |
class Music { | |
private String title; | |
private String artist; | |
private int time; | |
private int rating; | |
public static void main(String[] args) { | |
Music music = new Music("Blue-Love Chime", "nao", 317, 5); | |
System.out.println(music); | |
music.play(); | |
} | |
public Music(String title, String artist, int time, int rating) { | |
this.title = title; | |
this.artist = artist; | |
this.time = time; | |
this.rating = rating; | |
} | |
public String toString() { | |
// C#だったらFormatの後ろの型に関係無く{0}, {1},,,と書けるのに | |
// %sを呼ぶと後ろの引数はtoString()される | |
return String.format("aMusic(%s, %s, %s, %s)", title, artist, time, rating); | |
} | |
public void play() { | |
String star; | |
switch (getRating()) { | |
case 1: | |
star = "☆☆☆☆★"; | |
break; | |
case 2: | |
star = "☆☆☆★★"; | |
break; | |
case 3: | |
star = "☆☆★★★"; | |
break; | |
case 4: | |
star = "☆★★★★"; | |
break; | |
case 5: | |
star = "★★★★★"; | |
break; | |
default: | |
star = "☆☆☆☆☆"; | |
} | |
// printfやString.formatはStringBuilderで処理しているらしいのでStringで+を使うよりはよいかもしれない | |
System.out.printf("playing: %s by %s %s sec., Fav: %s", title, artist, time, star); | |
System.out.println(); | |
} | |
public String getTitle() { | |
return title; | |
} | |
public String getArtist() { | |
return artist; | |
} | |
public int getTime() { | |
return time; | |
} | |
public int getRating() { | |
return rating; | |
} | |
} | |
class Album { | |
private String title; | |
private String artist; | |
private ArrayList<Music> musicList = new ArrayList<>(); | |
public static void main(String[] args) { | |
Album album = new Album("シアワセは月より高く", "美郷あき"); | |
album.add(new Music("シアワセは月より高く", "美郷あき", 244, 4)); | |
album.add(new Music("あかるい恋のうた", "美郷あき", 302, 3)); | |
System.out.println(album); | |
album.play(); | |
} | |
public Album(String title, String artist) { | |
this.title = title; | |
this.artist = artist; | |
} | |
public String getTitle() { | |
return title; | |
} | |
public String getArtist() { | |
return artist; | |
} | |
public ArrayList<Music> getMusicList() { | |
return musicList; | |
} | |
public void add(Music music) { | |
this.musicList.add(music); | |
} | |
// mapToIntの引数のラムダ式でtimeが各項に入ったIntStreamを生成 | |
public int getTotalTime() { | |
return musicList.stream().mapToInt(x -> x.getTime()).sum(); | |
} | |
public double getRating() { | |
return musicList.stream().mapToInt(x -> x.getRating()).average().orElse(Double.NaN); | |
} | |
public void play() { | |
System.out.printf("Album playing : %s by %s%n", getTitle(), getArtist()); | |
System.out.printf("%s曲, ", musicList.size()); | |
System.out.printf("Total: %s sec., ", getTotalTime()); | |
System.out.printf("Rating: %s", getRating()); | |
System.out.println(); | |
// for (Music m : musicList) { | |
// m.play(); | |
// } | |
// や | |
// musicList.forEach(x -> x.play()); | |
// と同じ | |
musicList.forEach(Music::play); | |
System.out.println("End of Album"); | |
} | |
public String toString() { | |
return String.format("anAlbum(%s, %s, %s, %s, %s)", | |
getTitle(), getArtist(), musicList.size(), getTotalTime(), getRating()); | |
} | |
} | |
class MusicPlayer { | |
private String name; | |
private ArrayList<Album> albumList = new ArrayList<>(); | |
public static void main(String[] args) { | |
Album album1 = new Album("シアワセは月より高く", "美郷あき"); | |
album1.add(new Music("シアワセは月より高く", "美郷あき", 244, 4)); | |
album1.add(new Music("あかるい恋のうた", "美郷あき", 302, 3)); | |
Album album2 = new Album("スケッチスイッチ", "ゆの(阿澄佳奈)、宮子(水橋かおり)、ヒロ(後藤邑子)、沙英(新谷良子)"); | |
album2.add(new Music("スケッチスイッチ", "ゆの(阿澄佳奈)、宮子(水橋かおり)、ヒロ(後藤邑子)、沙英(新谷良子)", 291, 5)); | |
album2.add(new Music("おんなのこパズル", "ゆの(阿澄佳奈)", 231, 3)); | |
Album album3 = new Album("ETERNAL BLAZE", "水樹奈々"); | |
album3.add(new Music("ETERNAL BLAZE", "水樹奈々", 309, 5)); | |
album3.add(new Music("RUSH&DASH!", "水樹奈々", 207, 3)); | |
album3.add(new Music("inside of mind", "水樹奈々", 263, 5)); | |
MusicPlayer player = new MusicPlayer("COWON S9"); | |
player.add(album1); | |
player.add(album2); | |
player.add(album3); | |
player.play(); | |
System.out.println(); | |
player.shufflePlay(); | |
} | |
public MusicPlayer(String name) { | |
this.name = name; | |
} | |
public void add(Album album) { | |
albumList.add(album); | |
} | |
public void play() { | |
System.out.printf("%sで全曲再生%n", name); | |
albumList.forEach(Album::play); | |
} | |
public void shufflePlay() { | |
// C#だったらWriteLineにFormattableStringを渡せるから%nとかいらないのに | |
System.out.printf("%sでランダム再生%n", name); | |
// C#だったらStreamとListの相互変換とかいらないのに | |
List<Music> allMusic = albumList | |
.stream() // Stream<Album>を生成して | |
.flatMap(x -> x.getMusicList().stream()) // Album#getMusicList()#stream() でStream<Music>に変換してflatMapでまとめる | |
.collect(Collectors.toList()); | |
Collections.shuffle(allMusic); | |
allMusic.forEach(Music::play); | |
System.out.println("再生終了"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment