Javaの学習を始めて26日くらいの人のための問題集

Java

オブジェクト指向基本問題。ArrayListの練習。

Q1
ArrayListを使って猫集めゲームを作成せよ。
○Collectによって登場する猫のタイプはA,B,Cいずれかのランダムとする。
○遊ぶと親密度が1増える
詳しくは実行例を参照のこと。
■Catクラス
[static field]
任意
[instance field]
type //A or B or C
name //名前
intimacy //親密度
[constructor]
任意
[method]
任意
■アプリケーションクラス
実行例となるようにする

[実行例]

***Cat Collection***
1.Collect 2.Play 3.End>2
You do not have anyone to play with.
1.Collect 2.Play 3.End>1
A-type cat appeared!
Please name this cat>John
John joined!
1.Collect 2.Play 3.End>1
B-type cat appeared!
Please name this cat>Paul
Paul joined!
1.Collect 2.Play 3.End>1
B-type cat appeared!
Please name this cat>George
George joined!
1.Collect 2.Play 3.End>2
0・・・John[A](0)
1・・・Paul[B](0)
2・・・George[B](0)
Who do you play with?>1
Playing with Paul
...
Intimacy is up!!
1.Collect 2.Play 3.End>2
0・・・John[A](0)
1・・・Paul[B](1)
2・・・George[B](0)
Who do you play with?>0
Playing with John
...
Intimacy is up!!
1.Collect 2.Play 3.End>2
0・・・John[A](1)
1・・・Paul[B](1)
2・・・George[B](0)
Who do you play with?>0
Playing with John
...
Intimacy is up!!
1.Collect 2.Play 3.End>3
Game over

Catクラス

public class Cat {
	public static final char[] TYPES={'A','B','C'};
	private char type;
	private String name;
	private int intimacy;
	public Cat(char type,String name){
		this.type=type;
		this.name=name;
	}
	
	public void play(){
		System.out.println("Playing with "+this.name+".");
		System.out.println("...");
		intimacy++;
		System.out.println("Intimacy is up!!");
	}
	public String info(){
		return String.format("%s[%c](%d)", this.name,this.type,this.intimacy);
	}
	public String getName(){
		return this.name;
	}
	public char getType(){
		return this.type;
	}
	public int getIntimacy(){
		return this.intimacy;
	}	
}

アプリケーションクラス

import java.util.*;
public class CatApp{
	public static void main(String[] args) {
		System.out.println("***Cat Collection***");
		Random r = new Random();
		Scanner s = new Scanner(System.in);
		ArrayList<Cat> list = new ArrayList<>();
		while (true) {
			System.out.print("1.Collect Cats 2.Play 3.End>");
			int select = s.nextInt();
			switch (select) {
			case 1:
				int index = r.nextInt(Cat.TYPES.length);
				char type = Cat.TYPES[index];
				System.out.println(type + "-type cat appeared!");
				System.out.print("Please name this cat>");
				String name = s.next();
				Cat c = new Cat(type, name);
				list.add(c);
				System.out.println(name + " joined!");
				break;
			case 2:
				if (list.size() == 0) {
					System.out.println("You do not have anyone to play with.");
				} else {
					for (int i = 0; i < list.size(); i++) {
						System.out.printf("%d・・・%s%n", i, list.get(i).info());
					}
					System.out.print("Who do you play with?>");
					int num = s.nextInt();
					list.get(num).play();
				}
				break;
			case 3:
				System.out.println("Game over");
				s.close();
				return;
			}
		}
	}
}

Q2(応用)
Playの際の一覧表示をIntimacy降順にせよ。

アプリケーションクラス

import java.util.*;
public class CatApp {
	private static ArrayList<Cat> list=new ArrayList<>();
	private static final int NAME_ASC=0,TYPE_ASC=1,INTIMACY_DESC=2;
	private static int mode=INTIMACY_DESC;
	public static void main(String[] args) {
		System.out.println("***Cat Collection***");
		Random r=new Random();
		Scanner s=new Scanner(System.in);		
		while(true){
			System.out.print("1.Collect 2.Play 3.End>");
			int select=s.nextInt();
			switch(select){
			case 1:
				int index=r.nextInt(Cat.TYPES.length);
				char type=Cat.TYPES[index];
				System.out.println(type+"-type cat appeared!");
				System.out.print("Please name this cat>");
				String name=s.next();
				Cat c=new Cat(type,name);
				list.add(c);
				System.out.println(name+" joined!");
				break;
			case 2:
				if(list.size()==0){
					System.out.println("You do not have anyone to play with.");
				}else{
					sortList(mode);
					for(int i=0;i<list.size();i++){
						System.out.printf("%d・・・%s%n",i,list.get(i).info());
					}
					System.out.print("Who do you play with?>");
					int num=s.nextInt();
					list.get(num).play();
				}			
				break;
			case 3:
				System.out.println("Game over");
				s.close();
				return;
			}
		}

	}
	private static void sortList(int sortMode){
		for(int i=0;i<list.size()-1;i++){
			for(int j=i+1;j<list.size();j++){
				if(
					sortMode== NAME_ASC && list.get(i).getName().compareTo(list.get(j).getName()) >0 ||
					sortMode == TYPE_ASC && list.get(i).getType() > list.get(j).getType() || 
					sortMode == INTIMACY_DESC && list.get(i).getIntimacy() < list.get(j).getIntimacy() 
					){
					Cat cat=list.get(i);
					list.set(i, list.get(j));
					list.set(j, cat);
				}
			}
		}
	}	 
}

コメント

タイトルとURLをコピーしました