Java实验1,2

来源:互联网 发布:德州淘宝招聘信息 编辑:程序博客网 时间:2024/06/05 04:15

1.  用对分法解方程 x^(1/2)=cos(x) (x>0)

a、先在纸上研究好算法

b、使用Math.sqrt(), Math.cos()

c、实型数的比较方法(不能用相等,要用差的绝对值很小作为判断)

d、--------

e、大致的结果区间

f、怎么结束,常量(到怎样的精度则停止循环计算)

g、取中值

2.  用欧几里德辗转相除法求两个正整数的最大公约数

a、先研究好算法,可以到网上查查

3.  利用级数展开式计算cos(x)

a、注意循环控制的设计

b、反复查找了源程序,没有问题,但运行的结果就是不对,试着从其他方面找原因

c、可以和Math.cos()的结果做一个比较

d、---------

e、不用数组,通项公式;

f、结束条件,常量

4.  编程计算1000以内的质数(输出格式:一行10个,按列右对齐)

a、注意设计好“信号变量”

5.  将任意一个字符串颠倒排列输出

a、引用正确的类及其方法

b、-------------------

c、主要是合理运用String或StringBuffer的方法以及循环控制

6.  符号三角形(输入--+-++-+)(第一行从第10列开始,整个三角形右对齐)

a、想办法控制好输出的格式

b、------------------

c、什么是符号三角形

d、如何控制起始位置

e、字符的比较,是否可以考虑用异或

7.  编程计算1^1+2^2+3^3+4^4+5^5+……+20^20

a、注意结果的大小,体会数据的范围;

       b、需要用到数组;

       c、-----------------------------------

       d、不能在运算过程中使用实型数

       e、使用数组,一个数组元素装一位

       f、要使用辅助方法。

 

 

目的:

1、熟悉程序结构

2、熟悉Java各种常用语句

3、熟悉几种Java常用方法的引用

4、培养好的源代码注释习惯

5、培养好的命名风格




import java.util.Arrays;public class test {public static void main(String[] args) {//实验一experimentOne();//实验2System.out.println("题目1:  x^(1/2)=cos(x)解为:  " + question0neAnswer());System.out.println();System.out.println("题目2:  用辗转相除法求105和252的最大公约数为:" + questionTwoAnswer(105,252));System.out.println();System.out.println("题目3:  利用级数展开式计算cos(x):" );System.out.println("Java cos(0.1)  Math函数: " + Math.cos(0.1)+"  程序:"+questionThreeAnswer(0.1));System.out.println("Java cos(-0.1) Math函数: " + Math.cos(-0.1)+"  程序:"+questionThreeAnswer(-0.1));System.out.println("Java cos(1)    Math函数: " + Math.cos(1)+"  程序:"+questionThreeAnswer(1));System.out.println("Java cos(-1)   Math函数: " + Math.cos(-1)+"  程序:"+questionThreeAnswer(-1));System.out.println("Java cos( 10)  Math函数: " + Math.cos(10)+"  程序:"+questionThreeAnswer(-10));System.out.println("Java cos(-10)  Math函数: " + Math.cos(-10)+"  程序:"+questionThreeAnswer(-10));System.out.println("Java cos(100)  Math函数: " + Math.cos(100)+"  程序:"+questionThreeAnswer(100));System.out.println("Java cos(-100) Math函数: " + Math.cos(-100)+"  程序:"+questionThreeAnswer(-100));System.out.println();System.out.println("题目4:  计算1000以内的质数:" );questionFourAnswer();System.out.println();System.out.println();System.out.println("题目5:  将一个字符串颠倒输出:" );System.out.println("原字符串为:Do You Want To Change This Sentence?" );String beforeChange = "Do You Want To Change This Sentence?";questionFiveAnswer(beforeChange);System.out.println();System.out.println("题目6:  符号三角形:" );String firstLine = "--+-++-+";questionSixAnswer(firstLine);System.out.println();System.out.println("题目7:编程计算1^1+2^2+……+n^n:" );for(int i = 1; i<=30; i++){System.out.println("当n="+i+"时:" );questionSevenAnswer(i);System.out.println();}}public static void experimentOne(){short theShort = 2;int theInt = 10;char theChar = 'A';long theLong = 20;float theFloat = 4.5f;double theDouble = 10.5;//设计至少10种不同类型的数据,运用+、-、*、/、%运算组合情况,//测试结果的数据类型并显示在console中System.out.println("2 = "+ Poly.getType( 2 ) );System.out.println("2.0 = "+ Poly.getType( 2.0 ) );System.out.println("char + int = "+ Poly.getType( theInt + theChar ) );System.out.println("char + short = "+ Poly.getType( theInt + theShort ) );System.out.println("short + short = "+ Poly.getType( theShort + theShort ) );System.out.println("short + int = "+ Poly.getType( theInt + theShort ) );System.out.println("int + int = "+ Poly.getType( theInt + theInt ) );System.out.println("int + long = "+ Poly.getType( theInt + theLong ) );System.out.println("short + long = "+ Poly.getType( theLong + theShort ) );System.out.println("long + long = "+ Poly.getType( theLong + theLong ) );System.out.println("int + float = "+ Poly.getType( theInt + theFloat ) );System.out.println("float + Double = "+ Poly.getType( theFloat + theDouble ) );System.out.println("int * float = "+ Poly.getType( theInt * theFloat ) );System.out.println("int / Double = "+ Poly.getType( theInt / theDouble ) );System.out.println();//设计至少6种数据被0和0.0除的情况, 观察运行结果,如果能显示在console中,则显示。theInt = 0;theFloat = 0;theDouble = 0;System.out.println("2.0/0 = "+ 2.0/0);System.out.println("-2.0/0 = "+ -2.0/0);System.out.println("2.0f/0 = "+ 2.0f/0);System.out.println("-2.0f/0 = "+ -2.0f/0);System.out.println("0.0/0 = "+ 0.0/0);System.out.println("-0.0/0 = "+ -0.0/0);System.out.println("0.0f/0 = "+ 0.0f/0);System.out.println("-0.0f/0 = "+ -0.0f/0);System.out.println();//设计至少5种char型数据和int型数据之间相互转换的情况,显示在console中。theInt = theChar;System.out.println(theInt); //将char转换为int,theInt = theChar + '0';System.out.println(theInt); System.out.println(Integer.toString(theInt)); //将char转换为int,用方法System.out.println(Integer.toString(theInt+'A'));theInt = 65;theChar = (char)theInt;System.out.println(theChar); //将int转换为char  强制转换System.out.println(theChar+ '0');System.out.println( (char)(theChar+ '0'));}public static float question0neAnswer(){ /*  用对分法解方程 x^(1/2)=cos(x) (x>0)        范围确定:下限是0,x = (cos x)^2  所以x<1,为上限, 而且函数f(x) = x^(1/2)=cos(x)是递增函数*/    float theMax = 1;    float theMin = 0;float theMid = (theMax + theMin)/2;while( Math.abs( Math.sqrt(theMid) - Math.cos(theMid) ) > 0.000001){if( Math.sqrt(theMid) - Math.cos(theMid) > 0 ){theMax = theMid;}else{theMin = theMid;}theMid = (theMax + theMin)/2;}return theMid;}public static int questionTwoAnswer(int theFirstNumber, int theSecondNumber){//用欧几里德辗转相除法求两个正整数的最大公约数    //一直也保持第二个数字较大。int temp;while( theSecondNumber/theFirstNumber > 0 && (temp = theSecondNumber%theFirstNumber) >0){theSecondNumber = theFirstNumber;theFirstNumber = temp;}return theFirstNumber;}public static double questionThreeAnswer(double d){    //利用级数展开式计算cos(x)//1-x2/2!+x4/4!-x6/6!+…-(-1)n+1xn/n!double cos = 1;if(d<0){d=-d;}while(d>7){d-=2*Math.PI;}for(int i = 2; i<=20; i+=2){long temp = 1;for(int j = 1; j<=i; j++){temp*=j;}if(i%4==2){cos-=Math.pow(d,i)/temp;}else{cos+=Math.pow(d,i)/temp;}}return cos;}public static void questionFourAnswer(){//计算1000以内的质数int count = 1;for(int theNumber = 2; theNumber < 1001; theNumber++){if( isAPrime(theNumber) == true ){System.out.printf("%5d",theNumber); //控制输出右对齐,且每一个数字占5个字符的长度if( count++ % 10 == 0){System.out.println();}}}}public static boolean isAPrime(int number){boolean isPrime = true;for(int i = 2; i<number; i++){if( number%i == 0 ){isPrime = false;break;}}return isPrime;}public static void questionFiveAnswer(String beforeChange){StringBuffer AfterChange = new StringBuffer();for(int i = beforeChange.length()-1; i>-1; i--){AfterChange.append(beforeChange.charAt(i));}System.out.println("转换后为:"+AfterChange);}public static void questionSixAnswer(String firstLine){//直接判断两个符号是不是相等,不等为-,相等为+//temp保存上一行的信息,answer保存当前输出行的信息。StringBuffer temp = new StringBuffer();StringBuffer answer = new StringBuffer();temp.append(firstLine);System.out.println("          " + temp);  //先输出第一行for(int i = 0; i<firstLine.length()-1; i++){for(int j = 0; j<11+i; j++){System.out.print(" ");}for(int j = 0; j<temp.length()-1; j++){if( temp.charAt(j) != temp.charAt(j+1) ){System.out.print("-");answer.append('-');}else{System.out.print("+");answer.append('+');}} System.out.println();temp.delete(0,temp.length());temp.append(answer);answer.delete(0,answer.length());}}public static void questionSevenAnswer(int n){//编程计算1^1+2^2+……+n^nint result[] = new int[100];int temp[] = new int[100];for(int i = 1; i<n+1; i++){//先算temp...单值Arrays.fill(temp, 0);temp[0] = i%10;temp[1] = i/10;for(int j = 0; j<i-1; j++){count(temp, i);}add(temp, result);}int i = 99;while(result[i--] == 0);for(i = i+1 ; i>=0; i--){System.out.print(result[i]);}}public static void count(int[]  temp, int i){int toNext = 0;for(int j = 0; j<100; j++){int intTemp = toNext;toNext = (temp[j]*i+toNext)/10;temp[j] =(temp[j]*i+intTemp)%10;}}public static void add(int[]  temp, int[]  result){int toNext = 0;for(int j = 0; j<100; j++){int intTemp = toNext;toNext = (result[j]+temp[j]+toNext)/10;result[j] = (result[j]+temp[j]+intTemp)%10;}}}class Poly  {    private final static String INT_TYPE = "int";    private final static String LONG_TYPE = "long";    private final static String DOUBLE_TYPE = "double";    private final static String FLOAT_TYPE = "float";    private final static String CHAR_TYPE = "char";    private final static String BYTE_TYPE = "byte";    private final static String SHORT_TYPE = "short";    private final static String BOOLAEN_TYPE = "boolean";    public static String getType(int i) {return INT_TYPE;}    public static String getType(long l) {        return LONG_TYPE;    }    public static String getType(double d) {        return DOUBLE_TYPE;    }    public static String getType(float f) {        return FLOAT_TYPE;    }    public static String getType(char c) {        return CHAR_TYPE;    }    public static String getType(byte by) {        return BYTE_TYPE;    }    public static String getType(short s) {        return SHORT_TYPE;    }    public static String getType(boolean bo) {        return BOOLAEN_TYPE;    }    public static String getType(Object obj){        return obj != null ? obj.toString().split("@")[0] : null;    }}


0 0
原创粉丝点击