11道の簡単なJAVA初心者の練習問題

68458 ワード

1,Javaプログラムを作成し,キーボードから10個の数を入力し,長さ10の配列に値を与え,バブルソート法により配列をソートする.
import java.util.Scanner;
public class Demo1 {

	public static void main(String[] args) {
		int[] a=new int[10];
		Scanner sc = new Scanner(System.in);
		System.out.println("   10  ");
		for(int i=0;i<a.length;i++) {
			System.out.println("    "+(i+1)+"  ");
			a[i]=sc.nextInt();
		}
		for(int i=0;i<a.length-1;i++) {
			for(int j=0;j<a.length-1-i;j++) {
				if(a[j]>a[j+1]) {
					int temp=a[j];
					a[j]=a[j+1];
					a[j+1]=temp;
				}
			}
		}
		System.out.println("      ");
		for(int i=0;i<a.length;i++) {
			System.out.print(a[i]+"\t");
		}
	}

}

2,101−200の間に何個の素数があるかを判断し,すべての素数を出力する.
public class Demo2 {

	public static void main(String[] args) {
		int count=0;
		int[] a=new int[50];
		for(int i=101;i<=200;i++) {
			if(isPrime(i)) {
				a[count]=i;
				count++;
			}
		}
		System.out.println("101-200    "+count+"   ");
		System.out.println("     :");
		for(int i=0;i<count;i++) {
			System.out.print(a[i]+"\t");
		}

	}
	public static boolean isPrime(int num) {
		boolean isprime=true;
		int len=num/2;
		for(int i=2;i<len;i++) {
			if(num%i==0) {
				isprime=false;
				break;
			}
		}
		if(num==2) {
			return true;
		}
		return isprime;
	}
}

3,プログラムを編纂して、1を求めます!+2!+3!+…n!の和、nの値はキーボードで入力します.
import java.util.Scanner;
public class Demo3 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println(" 1!+2!+3!+...n!  ,   n  ");
		int n=sc.nextInt();
		int sum=0;
		for(int i=1;i<=n;i++) {
			sum+=Fac(i);		
		}
		System.out.println("   "+sum);
	}
	public static int Fac(int num) {
		int mul=1;
		for(int i=num;i>0;i--) {
			mul*=i;
		}
		return mul;
	}

}

4,プログラムを作成し,1つの偶数を2つの素数の和に分解して出力する.
import java.util.Scanner;
public class Demo4 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("        ");
		int n;
		while(true) {
			n = sc.nextInt();
			if(n<=0||n%2!=0) {
				System.out.println("         ,     ");
			}
			else if(n==2){
				System.out.println("2    ");
			}
			else {
				break;
			}
		}

		for(int i=2;i<n/2;i++) {
			 if(isPrime(i)&&isPrime(n-i)) {
				 System.out.println(n+"="+i+"+"+(n-i));
				 break;
			 }
		}
	}
	public static boolean isPrime(int num) {
		boolean isprime=true;
		int len=num/2;
		for(int i=2;i<len;i++) {
			if(num%i==0) {
				isprime=false;
				break;
			}
		}
		if(num==2) {
			return true;
		}
		return isprime;
	}
}

5,プログラムを記述し,mとnの最大公約数と最小公倍数を求める.
import java.util.Scanner;
public class Demo5 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int m,n;
		System.out.println("      ");
		System.out.println("       ");
		m=sc.nextInt();
		System.out.println("       ");
		n=sc.nextInt();
		System.out.println("      :"+gcd(m,n));
		System.out.println("      :"+lcm(m,n));
	}
	public static int gcd(int m,int n) {
		while(m%n!=0) {
			int r = m%n;
			m = n;
			n = r;
		}
		return n;
	}
	public static int lcm(int m,int n) {
		
		return m*n/gcd(m,n);
	}

}

6、文字列str 1とstr 2を定義し、str 1はStringタイプ、str 2はStringBufferタイプであり、初期化し、以下の操作を行う:(1)2つの文字列の長さを出力する;(2)str 1を逆順に出力し、例えばstr 1=「today」であれば「yadot」を出力する.(3)文字列str 2を右に3ビット移動し、例えばstr 2=「todayisgood」であれば「oodtodayisg」を出力する.
public class Demo6 {

	public static void main(String[] args) {
		String str1="today";
		StringBuffer str2 = new StringBuffer("todayisgood");
		System.out.println("str1    "+str1.length());
		System.out.println("str2    "+str2.length());
		System.out.println("str1     ");
		for(int i = str1.length()-1;i>=0;i--) {
			char a = str1.charAt(i);
			System.out.print(a);
		}
		System.out.println("
str2 :"
); for(int j = 0;j<str2.length();j++) { System.out.print(str2.charAt((str2.length()+j-3)%str2.length())); } } }

7,プログラム作成,選択法による配列ソート
public class Demo7 {

	public static void main(String[] args) {
		int[] arr={1,3,2,45,65,33,12};
        System.out.println("   :");
        for(int num:arr){
            System.out.print(num+" ");
        }        

        for(int i = 0; i < arr.length - 1; i++) {
            int k = i;
            for(int j = k + 1; j < arr.length; j++){
                if(arr[j] < arr[k]){ 
                    k = j;
                }
            }

            if(i != k){
                int temp = arr[i];
                arr[i] = arr[k];
                arr[k] = temp;
            }    
        }
        System.out.println();
        System.out.println("   :");
        for(int num:arr){
            System.out.print(num+" ");
        }

	}

}

8、正の整数を素因数に分解します.例えば、90を入力し、90=2*3*3*5を印刷します.
import java.util.Scanner;
public class Demo8 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("        ");
		int m = sc.nextInt();
		int n = 2;
		System.out.print(m+"=");
		while(n<=m) {
			if(n==m) {
				System.out.print(m);
	            break;
			}
			else if(m%n==0) {
				System.out.print(n+"*");
				m/=n;
			}
			else {
				n++;
			}
		}
		

	}

}

9,プライベート属性name,age,公開メソッドvoid showInfo()を含むクラスAnimalを記述する.Animalクラスにコンストラクタとプロパティアクセサを追加します.プライマリ・クラスDemo 9を定義し、プライマリ・メソッドでAnimalクラスをインスタンス化し、コンストラクタとプロパティ・アクセサでそれぞれnameとageに値を割り当て、showInfoメソッドを呼び出してプロパティ情報を出力します.
public class Demo9 {

	public static void main(String[] args) {
		Animal a = new Animal(" ");
		a.setAge(5);
		a.showInfo();

	}

}

public class Animal {
	private String name;
	private int age;
	public Animal(String name) {
		this.name=name;
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public void showInfo(){
		System.out.println("   :"+name);
		System.out.println("   :"+age+" ");
	}
	
}

10は、共通属性name、age、共通メソッドvoid showInfo()を含むクラスPersonを記述し、メソッドは2つの属性nameとageを出力する.PersonクラスのサブクラスStudentクラスを記述し,このクラスでは共通属性StuIDを定義し,親クラスのshowInfoメソッドを書き換え,3つの属性の情報を出力する.マスタークラスを定義し、マスターメソッドでStudioクラスとPersonクラスをインスタンス化し、各プロパティに値を割り当て、showInfo()メソッドを呼び出してすべてのプロパティ情報を出力します.
public class Demo10 {

	public static void main(String[] args) {
		Person p = new Person("  ",25);
		Student s = new Student("   ",18,"12332100");
		p.showInfo();
		s.showInfo();

	}

}

public class Person {
	public Person() {
	}
	public Person(String name, int age) {
		this.name=name;
		this.age=age;
	}
	public String name;
	public int age;
	public void showInfo() {
		System.out.println("   :"+name);
		System.out.println("   :"+age+" ");
	}
}

public class Student extends Person{
	public Student(String name, int age, String StuID) {
		this.name=name;
		this.age=age;
		this.StuID=StuID;
	}
	public String StuID;
	public void showInfo() {
		System.out.println("   :"+name);
		System.out.println("   :"+age+" ");
		System.out.println("ID :"+StuID);
	}
}

11.シンボル定数PI、抽象メソッドgetArea()を含むインタフェースShapeを記述する.2つの一般クラスRectangleとCircleを定義します.両方のクラスはShapeインタフェースを宣言する必要があります.RectangleクラスではgetArea()メソッドを複写して矩形面積を求め,CircleクラスではgetArea()メソッドを複写して円形面積を求める.主関数で2つのクラスの例をそれぞれ定義し、矩形辺長入力4と6、円形入力半径値3で、それぞれの面積を求めて出力します.
import java.util.Scanner;
public class Demo11 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		double a,b,r;
		System.out.println("       ");
		a=sc.nextDouble();
		System.out.println("       ");
		b=sc.nextDouble();
		System.out.println("        ");
		r=sc.nextDouble();
		Rectangle rec = new Rectangle(a,b);
		Circle cir = new Circle(r);
		System.out.println("      "+rec.getArea());
		System.out.println("      "+cir.getArea());
	}

}

public interface Shape {
	public static final double PI=3.14;
	public abstract double getArea();
}

public class Rectangle implements Shape{
	private double a;
	private double b;
	public Rectangle(double a, double b) {
		this.a=a;
		this.b=b;
	}


	public double getArea() {
		return a*b;
	}
}

public class Circle implements Shape{
	double r;

	public Circle(double r) {
		this.r=r;	
	}
	public double getArea() {
		return PI*r*r;
	}
}