java语言试题
1.1!+2!+3!+…+n !
package 实验报告一;
import java.math.*;
public class 第一题{
public static void main(String args[])
{
BigInteger sum=new BigInteger ("0"),
a=new BigInteger ("1"),
one=new BigInteger ("1"),
b = new BigInteger ("1"),
c=new BigInteger ("9");
while(https://www.360docs.net/doc/1811630800.html,pareTo(c)<=0) {
sum=sum.add(b);
a=a.add(one);
b=b.multiply(a);
}
System.out.println("1!+2!+....+9!="+sum);
}
}
3.利用二分查找算法对一个有序数列进行查找. package 实验报告一;
import java.util.*;
import java.util.Arrays;
public class 第三题{
public static void main(String args[]) {
int []
a={12,34,9,23,45,6,45,90,123,19,34};
Arrays.sort(a);
System.out.println(Arrays.toString(a));
int number=90;
int
index=Arrays.binarySearch(a,number);
if(index>=0){
System.out.println(number+"和数组中索引为"+index+"的元素值相同");
}
else{
System.out.println(number+"不与数组中任何元素值相同");
}
}
} 2.产生100个随机数并使用冒泡排序和选择排序对这些数进行排序.
package 实验报告一;
import java.util.*;
public class 第二题{
public static void main(String args[]) {
int a[]=new int[100];
int s[];
Random random=new Random();
for(int i=0;i<100;i++) {
a[i]=random.nextInt(100);
}
s=Arrays.copyOf(a, 100);
for(int i = 0;i<100-1;i++)
{ int k=i;
for(int j = i+1;j < 100;j++)
{
if(a[k]>a[j])
k=j;
}
if(k!=i)
{ int temp;
temp = a[k];
a[k] = a[i];
a[i] = temp;
}
}
System.out.println("选择排序的结果:");
System.out.println(Arrays.toString(a));
for(int i=1;i<100;i++)
for (int j = 0; j < 100 - i; j++)
{
if (s[j]>s[j + 1]) {
int temp;
temp = s[j];
s[j] = s[j + 1];
s[j + 1] = temp;
}
}
System.out.println("冒泡排序的结果:");
System.out.println(Arrays.toString(s));
}
}
4.不同进制之间的转换
package 实验报告一;
public class 第四题{
public static void main(String args[])
{
int number=8642;
System.out.println(number+"的2进制表示"+Long.toString(number, 2));
System.out.println(number+"的4进制表示"+Long.toString(number, 4));
System.out.println(number+"的8进制表示"+Long.toString(number, 8));
System.out.println(number+"的16进制表示"+Long.toString(number, 16));
}
} 5.. 编写Java 程序用于显示人的姓名和年龄。定义一个人类Person。该类中应该有两个私有属性:姓名(name)和年龄(age)。定义构造方法用来初始化数据成员。再定义显示(display())方法将姓名和年龄打印出来。在main 方法中创建人类的实例然后将信息显示。
public class Person {
private String name;
private int age;
Person( String name,int age)
{
this. name=name;
this.age=age;
}
public void display(){
System.out.println(name+age);
}
}
2.构造方法与重载:定义一个网络用户类,要处理的信息有用户ID、用户密码、email 地址。在建立类的实例时把以上三个信息都作为构造函数的参数输入,其中用户ID 和用户密码时必须缺省时email地址是用户ID 加上字符串
package 实验报告二第二题;
public class Sb{
String ID;
String password;
String email;
public Sb(String ID,String password,String email){ this.ID=ID;
this.password=password;
this.email=email;
}
public Sb(String ID,String password){
this.ID=ID;
email=ID+"@https://www.360docs.net/doc/1811630800.html,";
this.password=password;
}
public static void main(String args[]) {
Sb ID1=new Sb("小明","123456", "123456@https://www.360docs.net/doc/1811630800.html,");
System.out.println(ID1.ID+"\n"+ID1.password+" \n"+ID1.email);
Sb ID2=new Sb("小红","1234567");
System.out.println(ID2.ID+"\n"+ID2.password+" \n"+ID2.email);
}
}
public class 第一题{
public static void main(String[] args) {
Bird bird = new Bird("白色", 6);
Fish fish = new Fish("10斤", 2);
https://www.360docs.net/doc/1811630800.html,();
https://www.360docs.net/doc/1811630800.html,();
}
}
abstract class Animal{ 3.定义一个表示学生信息的类Student,要求如下:(1)类Student 的属性如下:
sNO 表示学号;sName 表示姓名;sSex 表示性别;sAge 表示
年龄;sJava:表示Java 课程成绩。
(2)类Student 带参数的构造方法:
在构造方法中通过形参完成对成员变量的赋值操作。
(3)类Student 的方法成员:
getNo():获得学号;
getName():获得姓名;
getSex():获得性别;
getAge()获得年龄;getJava():获得Java 课程成绩
package 实验报告二第三题;
public class Student {
int sNo;
String sName;
String sSex;
int sAge;
double sJava;
public Student(int sNo,String sname,String sSex,int sAge,double sJava)
{
this.sNo=sNo;
this.sName=sname;
this.sSex=sSex;
this.sAge=sAge;
this.sJava=sJava;
}
public int getNo() {
return sNo;
}
public String getName() {
return sName;
}
public String getSex() {
return sSex;
}
public int getAge() {
return sAge;
}
public double getJava() {
return sJava;
int age ;
abstract public void info();
}
class Bird extends Animal{
String colour;
Bird(String colour, int age) {
this.colour = colour;
this.age = age;
}
public void info() {
System.out.println("我是一只" + colour + "的鸟!");
System.out.println("今年" + age + "岁了!");
}
}
class Fish extends Animal {
String weight;
Fish(String weight, int age) {
this.weight = weight;
this.age = age;
}
public void info() {
System.out.println("我是一只" + weight + "重的鱼!");
System.out.println("今年" + age + "岁了!");
}
}
}
public static void main(String args[]) {
Student []a=new Student[5];
a[0]=new Student(5, "小明", "男", 15, 96.0);
a[1]=new Student(6, "小红", "女", 16, 94.0);
a[2]=new Student(7, "小李", "男", 17, 91.0);
a[3]=new Student(8, "小华", "女", 18, 93.0);
a[4]=new Student(9, "小张", "男", 19, 99.0);