实验一
定一个类MyArray,满足如下要求:
1)含有私有成员变量int m_Ary[8]={821,2017,9,55,1000,821,2017,1};
2)在类中使用静态成员函数对外提供:①求数组中最大值,②数组中最小值,③数组长度,④数组排序,⑤数组显示的功能函数。
实验二
定一个类MyFactorial用于提供阶乘计算功能,满足如下要求:
1) 在类中使用静态成员函数对外提供:①计算指定输入正整数的阶乘,返回计算结果。
2) 主函数(main)中,接收键盘输入的整数,调用步骤1)定义的成员函数获取阶乘计算结果,并打印输出到屏幕。
实验三
定一个类,满足如下要求:
1)从键盘接收任意类型的数(double、float、int、short等)存储到成员变量(数组)中,该变量定义为私有的;
2)调用成员方法”MySort(待排序的数组作为参数)”对数组从小到大排序;
3)在主函数中输出排序后的数组。
实验四
父类MyPrint,包含show()方法,用于输出图形的形状。
子类MyPrintSquare,重写show ()方法,用’*’打印出边长为5的正方形;
子类MyPrintCircle,重写show ()方法, 用’*’打印出半径为5的圆。
测试类,设计一个myshow(MyPrint a)方法,实现输出的功能:如果为MyPrintSquare, 输出边长为5的正方形,如果为MyPrintCircle对象,输出半径为5的圆;主函数中创建MyPrintSquare、MyPrintCircle的对象,分别调用myshow,检查输出结果。
实验一
import java.util.Arrays;
public class MyArray {
private int m_Ary[]={821,2017,9,55,1000,821,2017,1};
public static int max(int[] array){
if(array==null || lengthOfArray(array)==0)
return Integer.MIN_VALUE;
int ret = array[0];
for(int t : array){
if(t>ret)
ret=t;
}
return ret;
}
public static int min(int[] array){
if(array==null || lengthOfArray(array)==0)
return Integer.MAX_VALUE;
int ret = array[0];
for(int t : array){
if(t<ret)
ret=t;
}
return ret;
}
public static int lengthOfArray(int[] array) {
return array.length;
}
public static void sort(int[] array){
Arrays.sort(array);
}
public static void printArray(int[] array){
for(int i:array){
System.out.print(i+" ");
}
System.out.println();
}
}
实验二
import java.util.Scanner;
public class MyFactorial {
public static long factorial(int i){
if(i==1||i==0)return 1;
long tmp = factorial(i-1);
System.out.println("current tmp is "+ i +" * " + tmp);
return i*tmp;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int num = scan.nextInt();
System.out.println(factorial(num));
}
}
实验三
import java.util.ArrayList;
import java.util.Comparator;
public class CombinedArray {
ArrayList<Object> array = new ArrayList<Object>();
public void addToArray(Object o){
array.add(o);
}
public void MySort(){
array.sort(new Comparator(){
@Override
public int compare(Object arg0, Object arg1) {
double a0 = Double.parseDouble(arg0.toString());
double a1 = Double.parseDouble(arg1.toString());
if( a0 > a1 )return 1;
else if(a0 < a1) return -1;
else return 0;
}
});
}
public static void main(String[] args) {
CombinedArray ca = new CombinedArray();
double a = 2.0;
float b =(float) 3.0;
int c = 4;
short d = 5;
ca.addToArray(d);
ca.addToArray(b);
ca.addToArray(c);
ca.addToArray(a);
ca.MySort();
for(Object o : ca.array){
System.out.print(o+ " " );
}
System.out.println();
}
}
实验四
abstract class MyPoint {
public abstract void show();
}
class MyPrintSquare extends MyPoint {
@Override
public void show() {
for(int i=0;i<5;++i){
for(int j=0;j<5;++j){
if(j==0 || j==4)
System.out.print('*');
else if(i==0 || i==4)
System.out.print('*');
else System.out.print(' ');
}
System.out.println();
}
}
}
class MyPrintCircle extends MyPoint{
@Override
public void show() {
String circle =
" ***** \n"+
" * * \n"+
"* *\n"+
"* *\n"+
"* *\n"+
"* *\n"+
"* *\n"+
" * * \n"+
" ***** \n";
System.out.print(circle);
}
}
public class MyPointTest {
public static void myShow(MyPoint a){
a.show();
}
public static void main(String[] args){
MyPoint mp1 = new MyPrintSquare();
MyPoint mp2 = new MyPrintCircle();
myShow(mp1);
myShow(mp2);
}
}