java第一次和第二次上机作业

Question

(1)编写一个Java程序,设计一个半径为3.0的圆的周长和面积,并输出计算结果。

(2)编写一个Java程序,根据勾股定理计算一个给定底和高的直角三角形的斜边长。

Code

(1) package shiyan2;
public class Circle {
        public static void main(String[] args){
        final double PI = 3.14;
        double r = 3.0;
        double c = 2 * PI * r;
        double s = PI * r * r;
        System.out.println("周长为" + c);
        System.out.println("面积为" + s);
    }
}
(2)package  shiyan2
public class RightTri{
    //长
    private double length;
    //宽
    private double width;
    //斜边
    private double hypotenuse;
    public void setLength(double l){
    length = l;
    }
    public void setWidth(double w){
    width = w;
    }
    /*public double getLength(double length){
    return length;
    }
    public double getWidth(double width){
    return width;
    }*/
    public double getHypo(){
    return Math.hypot(length,width);
    }
    }
    public class TestRightTri{
    public static void main(String[] args){
    RightTri r = new RightTri();
    r.setLength(4);
    r.setWidth(3);
    r.getHypo();
    System.out.println(r.getHypo());
    }
    }
​ package shiyan2;
public class Circle {
        public static void main(String[] args){
        final double PI = 3.14;
        double r = 3.0;
        double c = 2 * PI * r;
        double s = PI * r * r;
        System.out.println("周长为" + c);
        System.out.println("面积为" + s);
    }
}
(2)package  shiyan2
public class RightTri{
    //长
    private double length;
    //宽
    private double width;
    //斜边
    private double hypotenuse;
    public void setLength(double l){
    length = l;
    }
    public void setWidth(double w){
    width = w;
    }
    /*public double getLength(double length){
    return length;
    }
    public double getWidth(double width){
    return width;
    }*/
    public double getHypo(){
    return Math.hypot(length,width);
    }
    }
    public class TestRightTri{
    public static void main(String[] args){
    RightTri r = new RightTri();
    r.setLength(4);
    r.setWidth(3);
    r.getHypo();
    System.out.println(r.getHypo());
    }
    }
​

Question

(1)编写一个Java程序,计算并输出1+2+...+100的结果。

(2)编写一个Java程序,计算并输出n的阶乘(设n=10)。

(3)编写程序,输出100~200间的所有素数,每行输出5个数。

有一对雌雄兔子,每两个月就繁殖一对雌雄兔子。问

n个月后共有多少对兔子。使用递归方法编写程序

Code

package shiyan3;
​
public class Add {
​
    /**
         * @param args
        */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            int sum = 0;
            for(int i = 1 ; i <= 100 ; i++){
            
                sum += i;
            }
            System.out.println("总和为:" + sum);
        }
​
}
(2)package shiyan3;
​
public class jiecheng {
​
        public static void main(String[] args) {
            int i,s=1;
            for(i=1;i<=10;i++)
            {   s=s*i;}
            System.out.println(s);// TODO Auto-generated method     stub
    }
}
(3)package shiyan3;
​
public class Prime {
​
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int count = 0;
        for(int i = 100;i< 2000;i++){
            if(prime(i)){
                    
                System.out.print(i + "\t");
                count++;
                if(count % 5 == 0){
                    
                    System.out.println("");
                }       
            }       
        }
    }
​
    public static boolean prime(int m){
        
        boolean b = true;
        for(int i = 2;i < m ;i++){
            
            if(m % i == 0 ){
                
                b = false;
            }
        }
        return b;
    }
}
(4) package shiyan3;
​
import java.util.Scanner;
​
public class Pairs {
​
/**
 * @param args
 */
public static int pairs(int m){
​
if(m == 1){
​
return 1;
}else{
​
return pairs(m-1) * 2;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stubScanner sc = new Scanner( System.in 
​
);
String line = sc.nextLine();
int i = Integer.parseInt(line);
System.out.println(pairs(i));
}
​
}
​ shiyan3;
​
public class Add {
​
    /**
         * @param args
        */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            int sum = 0;
            for(int i = 1 ; i <= 100 ; i++){
            
                sum += i;
            }
            System.out.println("总和为:" + sum);
        }
​
}
(2)package shiyan3;
​
public class jiecheng {
​
        public static void main(String[] args) {
            int i,s=1;
            for(i=1;i<=10;i++)
            {   s=s*i;}
            System.out.println(s);// TODO Auto-generated method     stub
    }
}
(3)package shiyan3;
​
public class Prime {
​
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int count = 0;
        for(int i = 100;i< 2000;i++){
            if(prime(i)){
                    
                System.out.print(i + "\t");
                count++;
                if(count % 5 == 0){
                    
                    System.out.println("");
                }       
            }       
        }
    }
​
    public static boolean prime(int m){
        
        boolean b = true;
        for(int i = 2;i < m ;i++){
            
            if(m % i == 0 ){
                
                b = false;
            }
        }
        return b;
    }
}
(4) package shiyan3;
​
import java.util.Scanner;
​
public class Pairs {
​
/**
 * @param args
 */
public static int pairs(int m){
​
if(m == 1){
​
return 1;
}else{
​
return pairs(m-1) * 2;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stubScanner sc = new Scanner( System.in 
​
);
String line = sc.nextLine();
int i = Integer.parseInt(line);
System.out.println(pairs(i));
}
​
}
​

 

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页