解答例 問題 1 変数 a が 3 以上でかつ 7 以下の場合 true と表示し そうでない場合は false と表示するプログラムである public class Prog061004_01 { int a; boolean b; a = Integer.parseInt(buf.readLine()); b = (a >= 3) && (a <= 7); System.out.println(b); 問題 2 public class Prog061004_02 { double a = Double.parseDouble(buf.readLine()); // 実数値を標準入力より取得 double b = Double.parseDouble(buf.readLine()); // 実数値を標準入力より取得 char op = buf.readline().charat(0); // 文字を標準入力より取得 double result = 0; switch(op){ case '+': result = a+b; case '-': result = a-b; case '*': result = a*b; case '/': result = a/b; System.out.println(" 結果 = "+result); 問題 3 1 から順に整数を足していき (1+2+3+...+n) その和が 100 を超えない最大の n を求めるプログラムである public class Prog061004_03 { int n = 1; int sum = 0; while(sum+n <= 100){ // 今までの和と n を足し 100 以下ならループ sum = sum + n; // 現在の和に n を足す n = n + 1; //n を一つ増やす 1
System.out.println(n -1); // 一つ前の n を出力 問題 4 public class Prog061004_04 { int n = Integer.parseInt(buf.readLine()); // 入力を標準入力より取得 int [] a = new int[n]; int sum = 0; for (int i = 0; i < n; i++){ // 入力データのカウント a[i] = Integer.parseInt(buf.readLine()); for (int i = 0; i < n; i++){ // 表示する文字数のカウントと表示 sum+=a[i]; System.out.println(" 総和 = "+sum); 問題 5 public class Prog061004_05 { int n = Integer.parseInt(buf.readLine()); // 入力を標準入力より取得 int [] no = new int[n]; int [] score = new int[n]; int sum = 0; for (int i = 0; i < n; i++){ // 入力データのカウント System.out.print(" 学籍番号 = "); no[i] = Integer.parseInt(buf.readLine()); System.out.print(" 得点 = "); score[i] = Integer.parseInt(buf.readLine()); for (int i = 0; i < n; i++){ // 得点の総和計算 sum+=score[i]; int average = sum/n; // 平均点の計算 System.out.println(" 平均点 = "+average); for (int i = 0; i < n; i++){ // 平均点以上の学籍番号の表示 if (score[i]>=average) System.out.println(" 学籍番号 = "+no[i]); 問題 6 public class Prog061004_06 { public static void main(string args[]) throws Exception { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.print(" 文字列 = "); String s = in.readline(); 2
System.out.print(" 検索文字 = "); char ch = in.readline().charat(0); System.out.print(" 検出場所 :"); boolean flag = false; for (int i = 0; i < s.length(); i++){ // 入力文字列のカウント if(ch == s.charat(i)) { System.out.print((i+1)+" "); flag = true; if(!flag) { System.out.print(" 検索文字なし "); System.out.println(" "); 問題 7 class Calculate { private int tax; // コンストラクタの宣言 Calculate (int tax) { this.tax = tax; // 税額計算 int calculatetax (int p) { return((int)((p * tax/100.0)+0.5)); public class Prog061004_07 { public static void main(string[ ] args) throws IOException { // 税率を標準入力より取得 System.out.print(" 税率 :"); int tax = Integer.parseInt(buf.readLine()); // インスタンスの生成 コンストラクタの実行 Calculate goods = new Calculate(tax); // 定価を標準入力より取得 System.out.print(" 定価 :"); int price = Integer.parseInt(buf.readLine()); // 税額計算の実行 表示 System.out.println(" 税額は " + goods.calculatetax(price)+" 円 "); 問題 8 class MyFamily { private String name; private int age; // コンストラクタの宣言 MyFamily (String name, int age) { this.name = name; this.age = age; // 個人情報の表示 void display() { 3
System.out.println("Name: "+name+",age: "+age); public class Prog061004_08 { public static void main (String[] args){ // インスタンスの生成 コンストラクタの実行 MyFamily father = new MyFamily("hiroshi",50); MyFamily mother = new MyFamily("midori",45); MyFamily brother = new MyFamily("taro",20); // 個人情報の表示 father.display(); mother.display(); brother.display(); 問題 9 class Triangle{ private double a, b, c; // コンストラクタの宣言 public Triangle(){ this.a = this.b = this.c =2; public Triangle( double a, double b, double c ){ this.a = a; this.b = b; this.c = c; // 面積の計算 結果の返却 public double getarea(){ double s; double area; s = ( a + b + c ) / 2.0; area = Math.sqrt( s * (s-a) * (s-b) * (s-c) ); return area; // 3 辺の返却 public String getside(){ return a+" "+b+" "+c; public class Prog061004_09 { public static void main(string args[]){ Triangle T1, T2; // インスタンスの生成 コンストラクタの実行 T1 = new Triangle(); T2 = new Triangle( 3,4,5 ); // 面積計算の実行 表示 System.out.println("3 辺 "+T1.getSide()+" の三角形の面積 :"+T1.getArea()); System.out.println("3 辺 "+T2.getSide()+" の三角形の面積 :"+T2.getArea()); 問題 10 class Ticket{ int numberofa = 10; int numberofb = 10; int valueofa = 1000; 4
int valueofb = 2000; public int getticket(int n){ return 0; class ATicket extends Ticket{ public int getticket(int n){ if(numberofa-n >= 0) { numberofa = numberofa-n; return valueofa*n; else { return -1; class BTicket extends Ticket{ public int getticket(int n){ if(numberofb-n >= 0) { numberofb = numberofb-n; return valueofb*n; else { return -1; 括弧が抜けておりました public class Prog061004_10{ BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.print(" チケット種別 = "); char c = in.readline().charat(0); // チケット種別を入力 System.out.print(" チケット枚数 = "); int n = Integer.parseInt(in.readLine()); // チケット枚数を入力 Ticket t = new Ticket(); switch(c){ case 'a': t = new ATicket(); case 'b': t = new BTicket(); System.out.println(" チケット種別が間違っています "); int v = t.getticket(n); // 購入価格の計算 if(v >= 0) { System.out.println(" 購入価格 ="+v); else { System.out.println(" 在庫枚数が不足しています "); 問題 11 class PC{ private int SpecOfMemory; private int SpecOfCPU; private int SpecOfDisk; //Mega byte //Giga Hz //Giga byte 5
public PC() { // 標準仕様の設定 setspecofmemory(256); //Mega byte setspecofcpu(2); //Giga Hz setspecofdisk(160); //Giga byte // メモリ仕様の設定 public void setspecofmemory(int m){ // メモリ仕様の設定 SpecOfMemory = m; public void setspecofcpu(int c){ // CPU 仕様の設定 SpecOfCPU = c; public void setspecofdisk(int d){ // ディスク仕様の設定 SpecOfDisk = d; public int getvalueofmemory(int n){ // メモリ価格の取得 int v = 0; switch(n){ case 256: v = 3000; case 512: v = 6000; System.out.println("Memoryの値が間違っています "); return v; public int getvalueofcpu(int n){ // CPU 価格の取得 int v = 0; switch(n){ case 2: v = 15000; case 4: v = 30000; System.out.println("CPUの値が間違っています "); return v; public int getvalueofdisk(int n){ // ディスク価格の取得 int v = 0; switch(n){ case 160: v = 9000; case 320: v = 15000; System.out.println("Diskの値が間違っています "); 6
return v; public int computevalue(){ return getvalueofmemory(specofmemory)+getvalueofcpu(specofcpu)+ getvalueofdisk(specofdisk) ; // a 仕様の設定 class PC_1 extends PC{ public PC_1() { setspecofmemory(512); //Mega byte setspecofdisk(320); //Giga byte // b 仕様の設定 class PC_2 extends PC_1{ public PC_2(){ setspecofcpu(4); //Giga Hz public class Prog061004_11{ BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.print("PCスペック種別 = "); String s = in.readline(); char c = 'd'; //PC 標準仕様の設定 if(s.length()!=0){ c = s.charat(0); //PCスペック種別を標準入力より取得 PC p = new PC(); switch(c){ case 'a': p = new PC_1(); case 'b': p = new PC_2(); int v = p.computevalue(); System.out.println("PC 価格 ="+v); 問題 12 interface Figure { public double getarea(); class Triangle implements Figure { double length1; double length2; double length3; public Triangle(double length1, double length2, double length3) { this.length1 = length1; this.length2 = length2; this.length3 = length3; public String tostring() { return " n Triangle : sides = " + length1 + ", " + length2 + ", " + length3 + " : " ; 7
public double getarea() { double lengthw = (length1 + length2 + length3) / 2.0; double trarea = Math.sqrt(lengthw * (lengthw - length1) * (lengthw - length2) * (lengthw - length3)); return trarea ; class Rectangle implements Figure { double length1; double length2; public Rectangle(double length1, double length2) { this.length1 = length1; this.length2 = length2; public String tostring() { return " n Rectangle : width = " + length1 + ", height = " + length2 + " : " ; public double getarea() { return length1 * length2 ; class Square extends Rectangle { public Square(double length1) { super(length1, length1); public String tostring() { return " n Square : width = " + length1 + " : "; public class Prog061004_12{ public static void main(string args[]) { Triangle t1 = new Triangle(2, 3, 3); Rectangle r1 = new Rectangle(5, 8); Square s1 = new Square(5); Figure[] figures = {t1, r1, s1; for (int i = 0; i < figures.length; i++) { System.out.println(figures[i] + " area = " + figures[i].getarea()); 8