《java入门程序题及源码.doc》由会员分享,可在线阅读,更多相关《java入门程序题及源码.doc(10页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、一. 实验目的掌握基本的输出格式学会使用集中循环结构对于类,对象有更深刻的了解,学会定义类,包括其中的变量,构造方法,类方法,等等二. 实验内容实验题目一: 找出15000范围内分别满足如下条件的数: (1) 7或11或13的倍数 (2) 7、11,或7、13或11、13的倍数 (3) 7、11和13倍数1 程序源代码 public class title1public static void main(String args)int z;int i=0;int j=0;int k=0;int aa,bb,cc;int length=2000;int a=new intlength,b=new
2、 intlength,c=new intlength;for(z=1;z=5000;z+)aa=z%7;bb=z%11;cc=z%13;if(aa=0|bb=0|cc=0)ai=z;i+;if(aa=0&bb=0)|(aa=0&cc=0)|(bb=0&cc=0)bj=z;j+;if(aa=0&bb=0&cc=0)ck=z;k+;System.out.printf(15000中是7或11或13的倍数的数有(共%d个):,i);System.out.println();for (z=0;zi;z+)System.out.printf(%d ,az);System.out.println();Sys
3、tem.out.printf(15000中是7、11,或7、13或11、13的倍数的数有(共%d个):,j);System.out.println();for (z=0;zj;z+)System.out.printf(%d ,bz);System.out.println();System.out.printf(15000中是7、11和13倍数的数有(共%d个):,k);System.out.println();for (z=0;zk;z+)System.out.printf(%d ,cz);2 实验结果实验题目二: 计算: y=3*1!/1+32*2!/22+33*3!/33+.+3n*n!/
4、nn。1 程序源代码import java.util.*;public class title2 public static double method(double n) double i=1,p=1,result=0; do p=p*i;result=result+Math.pow(3,i)*p/Math.pow(i,i);i+; while( i=n ); return result; public static void main(String args) Scanner sc =new Scanner(System.in);System.out.println( Result=3*1
5、!/1+32*2!/22+33*3!/33+.+3n*n!/nn:请输入n:);double k=sc.nextLong(); double Result=method(k); System.out.printf(Result=3*1!/1+32*2!/22+.+3%1.0f*%1.0f!/%1.0f%1.0f=%10.4f,k,k,k,k,Result); 2.实验结果实验题目三: 输出以下字符图形,比如,当n=6时,结果如下: 1 2 2 2 3 3 3 3 3 4 4 4 4 4 4 5 5 5 5 6 61 程序源代码import java.util.*;public class ti
6、tle3 public static void main(String args) Scanner sc =new Scanner(System.in);System.out.printf(显示n行字符图形,请输入n:);double n=sc.nextDouble();int t=(int)Math.floor(n/2); for(int i=1;i=n;i+) for(int j=1;j=Math.abs(t+1-i);j+) System.out.printf( ); int k=(t+1)i?(2*t+2-i):i;for(int j=1;j0?1:-1;n=Math.abs(n);d
7、=Math.abs(d);int t=gcd(n,d);numerator=n/t;denominator=d/t;static int gcd(int m,int n)int r=m%n;while(r!=0)m=n;n=r;r=m%n;return n;public Fraction add(Fraction r)int d=denominator*r.denominator;int n=sign*numerator*r.denominator+r.sign*r.numerator*denominator;return new Fraction(n,d);public Fraction m
8、inus(Fraction r)int d=denominator*r.denominator;int n=sign*numerator*r.denominator-r.sign*r.numerator*denominator;return new Fraction(n,d);public Fraction multiply(Fraction r)int d=denominator*r.denominator;int n=numerator*r.numerator;int s=sign*r.sign;return new Fraction(s*n,d);public Fraction divi
9、de(Fraction r)int d=denominator*r.numerator;int n=numerator*r.denominator;int s=sign*r.sign;return new Fraction(s*n,d);public void print()String str;if(numerator=0)System.out.println(numerator);else if(numerator%denominator=0)System.out.println(numerator/denominator);elsestr=sign*numerator+/+denomin
10、ator;System.out.println(str);package Rational;import java.util.*;public class TestFractionpublic static void main(String arg)Scanner sc =new Scanner(System.in);System.out.print(请输入第一个分数fra1的分子:);int n1=sc.nextInt();System.out.println();System.out.print(请输入第一个分数fra1的分母:);int d1=sc.nextInt();System.ou
11、t.println();System.out.print(请输入第二个分数fra2的分子:);int n2=sc.nextInt();System.out.println();System.out.print(请输入第二个分数fra2的分母:);int d2=sc.nextInt();System.out.println();Fraction fra1=new Fraction(n1,d1);Fraction fra2=new Fraction(n2,d2);Fraction fra3=fra1.add(fra2);Fraction fra4=fra1.minus(fra2);Fraction
12、 fra5=fra1.multiply(fra2);Fraction fra6=fra1.divide(fra2);System.out.print(第一个分数fra1=);fra1.print();System.out.print(第二个分数fra2=);fra2.print();System.out.print(两个分数的和fra1+fra2=);fra3.print();System.out.print(两个分数的差fra1-fra2=);fra4.print();System.out.print(两个分数的积fra1*fra2=);fra5.print();System.out.pri
13、nt(两个分数的商fra1/fra2=);fra6.print(); 2.试验结果实验题目五: 1 程序源代码package ComNum;public class Complexprivate double real;private double imaginary;public Complex(Complex r) real = r.real;imaginary = r.imaginary;public Complex(double re,double im)double t=mod(re,im);real=re;imaginary=im;public double mod(double
14、m, double n)return Math.sqrt(m*m+n*n);/ (a+bi) + (c+di) = (a+b) + (c+d)ipublic Complex add(Complex r)double newreal=real+r.real;double newimag=imaginary+r.imaginary;return new Complex(newreal,newimag);/ (a+bi) - (c+di) = (a-c) + (b-d)ipublic Complex minus(Complex r)double newreal=real-r.real;double
15、newimag=imaginary-r.imaginary;return new Complex(newreal,newimag);/(a+bi) * (c+di)=(ac-bd) + (bc+ad)ipublic Complex multiply(Complex r)double newreal=real*r.real-imaginary*r.imaginary;double newimag=imaginary*r.real+real*r.imaginary;return new Complex(newreal,newimag);/(a+bi) / (c+di)=(ac+bd)/(c*c+d
16、*d) + (bc-ad)/(c*c+d*d)ipublic Complex divide(Complex r)double newreal=(real*r.real+imaginary*r.imaginary)/(r.real*r.real+r.imaginary*r.imaginary);double newimag=(imaginary*r.real-real*r.imaginary)/(r.real*r.real+r.imaginary*r.imaginary);return new Complex(newreal,newimag);public void print()String
17、str;if(real=0)System.out.println(j*+imaginary);else if(imaginary=0)System.out.println(real);elseSystem.out.printf(%5.2f+j*%5.2f,real,imaginary);System.out.println();package ComNum;import java.util.*;public class TestComplexpublic static void main(String arg)Scanner sc =new Scanner(System.in);System.
18、out.print(请输入第一个复数num1的实部:);int real1=sc.nextInt();System.out.println();System.out.print(请输入第一个复数num1的虚部:);int imaginary1=sc.nextInt();System.out.println();System.out.print(请输入第二个复数num2的实部:);int real2=sc.nextInt();System.out.println();System.out.print(请输入第二个复数num2的虚部:);int imaginary2=sc.nextInt();Sy
19、stem.out.println();Complex num1=new Complex(real1,imaginary1);Complex num2=new Complex(real2,imaginary2);Complex num3=num1.add(num2);Complex num4=num1.minus(num2);Complex num5=num1.multiply(num2);Complex num6=num1.divide(num2);System.out.print(第一个复数num1=);num1.print();System.out.println();System.out
20、.print(第二个复数num2=);num2.print();System.out.println();System.out.print(两个复数的和num1+num2=);num3.print();System.out.println();System.out.print(两个复数的差numm1-num2=);num4.print();System.out.println();System.out.print(两个复数的积num1*num2=);num5.print();System.out.println();System.out.print(两个复数的商num1/num2=);num6.print();System.out.println(); 2.试验结果