【解答例】
【問題1】
変数 a が 3 以上でかつ 7 以下の場合、true と表示し、そうでない場合は false と表示するプログラムで ある。
import java.io.*;
public class Prog061004_01 {
public static void main(String[] args) throws IOException {
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in)); int a; boolean b; a = Integer.parseInt(buf.readLine()); b = (a >= 3) && (a <= 7); System.out.println(b); } } 【問題2】 import java.io.*;
public class Prog061004_02 {
public static void main(String[] args) throws IOException {
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in)); 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; break; case '-': result = a-b; break; case '*': result = a*b; break; case '/': result = a/b; break; } System.out.println("結果= "+result); } } 【問題3】 1 から順に整数を足していき(1+2+3+...+n)、その和が 100 を超えない最大の n を求めるプログラムである。 import java.io.*;
public class Prog061004_03 {
public static void main(String[] args) throws IOException { int n = 1; int sum = 0; while(sum+n <= 100){ //今までの和と n を足し、100 以下ならループ sum = sum + n; //現在の和に n を足す n = n + 1; //n を一つ増やす }
System.out.println(n -1); //一つ前の n を出力 }
}
【問題4】
import java.io.*;
public class Prog061004_04 {
public static void main(String[] args) throws IOException {
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in)); 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】 import java.io.*;
public class Prog061004_05 {
public static void main(String[] args) throws IOException {
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in)); 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】 import java.io.*;
public class Prog061004_06 {
public static void main(String args[]) throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.print("文字列= ");
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】 import java.io.*; 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 {
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in)); //税率を標準入力より取得
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() {
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】 import java.io.*; class Ticket{ int numberOfA = 10; int numberOfB = 10; int valueOfA = 1000;
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{
括弧が抜けておりました。 public static void main(String[] args) throws IOException {
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(); break; case 'b': t = new BTicket(); break; default: System.out.println("チケット種別が間違っています"); break; } int v = t.getTicket(n); //購入価格の計算 if(v >= 0) { System.out.println("購入価格="+v); } else { System.out.println("在庫枚数が不足しています"); } } } 【問題11】 import java.io.*; class PC{
private int SpecOfMemory; //Mega byte private int SpecOfCPU; //Giga Hz private int SpecOfDisk; //Giga byte
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; break; case 512: v = 6000; break; default: System.out.println("Memoryの値が間違っています"); break; } return v; }
public int getValueOfCPU(int n){ // CPU価格の取得 int v = 0; switch(n){ case 2: v = 15000; break; case 4: v = 30000; break; default: System.out.println("CPUの値が間違っています"); break; } return v; }
public int getValueOfDisk(int n){ // ディスク価格の取得 int v = 0; switch(n){ case 160: v = 9000; break; case 320: v = 15000; break; default: System.out.println("Diskの値が間違っています"); break; }
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{
public static void main(String[] args) throws IOException {
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(); break; case 'b': p = new PC_2(); break; default: break; } 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() {
}
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()); }
} }