Java实验

PS:一部分Java实验题目以及源代码未保存
1.字符串实验
分别显示1.01和1.001的整数部分和小数部分,注意精度保留

/**
 * 字符串实验
 * @author 15325
 *
 */
public class test1 {
public static void main(String[] args) {
	String a = "1.01";
	String b = "1.001";
	String [] a1 = new String[5];
	a1 = a.split("\\.");
	String [] a2 = new String[5];
	a2 = b.split("\\.");
	int c1,b1;
	double c2,b2,f1,f2,f3,f4;
	f1 = Double.parseDouble(a);
	f2 = Double.parseDouble(b);
	f1 = StrictMath.abs (f1);
	int p1 = (int)(f1);
	f3 =f1 - p1; 
	f2 = StrictMath.abs (f2);      
	int p2 = (int)(f2);
	f4 = f2 - p2;
	c1 = Integer.parseInt(a1[0]);
	b1 = Integer.parseInt(a2[0]);
	System.out.println("1.01的整数部分为:"+c1+"小数部分为:"+String.format("%.2f",f3));
	System.out.println("1.001的整数部分为:"+b1+"小数部分为:"+String.format("%.3f",f4));
}
}

2.泛型实验
1、定义三个数组,分别存储2个整型、2个浮点型和2个字符型元素。

2、设计通用方法print,能够遍历打印出三个数组的内容。

3、设计通用方法max,能够打印出整型数组、浮点型数组较大的元素。

package cn.sxr.lab;
import java.util.Scanner;
/**
 * 1、定义三个数组,分别存储2个整型、2个浮点型和2个字符型元素。

2、设计通用方法print,能够遍历打印出三个数组的内容。

3、设计通用方法max,能够打印出整型数组、浮点型数组较大的元素。
 * @author 15325
 *
 */
public class Test2 {				
	//通用方法myprint
		public static void myprint(int[] a) {
			System.out.println("数组中的元素为:");
			for(int i:a) {
				System.out.print(i);
				System.out.println(" ");
			}	
		}
			public static void myprint(double[] a) {
				System.out.println("数组中的元素为:");
				for(double i:a) {
					System.out.print(i);
					System.out.println(" ");
				}	
			}
				public static void myprint(char[] a) {
					System.out.println("数组中的元素为:");
					for(char i:a) {
						System.out.print(i);
						System.out.println(" ");
					}	
		
				}
	
		
		//通用方法max
				public static void max(int[] a) {
					int max=a[0];
					for(int k:a) {
						if(k>max) {
							max = k;
						}
					}
					System.out.println("max:"+max);
				}
				
				public static void max(double[] a) {
					double max=a[0];
					for(double k:a) {
						if(k>max) {
							max = k;
						}
					}
					System.out.println("max:"+max);
				}
				
				
				
		public static void main(String[] args) {
			int [] a = new int[2];
			a = new int[] {1,2};
			double [] b = new double[2];
			b = new double[] {1.1,2.2};
			char [] c = new char[2];
			c = new char[] {'A','B'};
			
			myprint(a);
			myprint(b);
			myprint(c);
			max(a);
			max(b);
			
		}
}

/*
for(int i=0;i<b.length;i++){
b[i]=in.nextInt();
}
*/
/*System.out.println("数组a中的元素为:");
for(int i:a) {
	System.out.print(i);
	System.out.println(" ");
}
System.out.println("数组b中的元素为:");
for(double j:b) {
	System.out.print(j);
	System.out.println(" ");
}

System.out.println("数组c中的元素为:");
for(char k:c) {
	System.out.print(k);
	System.out.println(" ");
}*/

3.异常实验

实验目的:熟悉异常的种类和使用。

实验要求:1、设计一个ATM存取钱程序,有本金1000.

              2、能够存钱、取钱、查询余额。

              3、自己设计余额不足异常,如果取钱大于余额,触发余额不足异常。
package cn.sxr.test;

public class IllegalnumException extends Exception{
	public IllegalnumException() {
		// TODO Auto-generated constructor stub
	}
	public IllegalnumException(String msg) {
		super(msg);
	}

}

package cn.sxr.test;

import java.util.Scanner;

/**
 * 综合实验七、异常实验

实验目的:熟悉异常的种类和使用。

实验要求:1、设计一个ATM存取钱程序,有本金1000.

                  2、能够存钱、取钱、查询余额。

                  3、自己设计余额不足异常,如果取钱大于余额,触发余额不足异常。
 * @author 15325
 *
 */
public class Bank {
	public static double money;
	
	//构造函数	
	public Bank() {
		money = 1000;
	}
	
	//存钱
	public static double cun() {
		System.out.println("请输入您要存入的钱数:");
		Scanner sc11 = new Scanner(System.in);
		double num = sc11.nextDouble(); 
		money = money + num;
	    System.out.printf("成功存入:%.2f,账户剩余:%.2f \n", num,money);
		return money;
	}
	
	//取钱
	public static double qu() throws IllegalnumException{		
		System.out.println("请输入您要取出的钱数:");
		Scanner sc11 = new Scanner(System.in);
		double num = sc11.nextDouble();
		if(num>money) {
			throw new IllegalnumException("余额不足");
		}
		//test(num);
		money = money - num;		
		System.out.printf("成功取出:%.2f,账户剩余:%.2f \n", num,money);
		return money;
	}
	/*public static void test(double num) throws Exception{
		try {
			if(num>money);
		}
		catch(Exception e){
			throw new Exception();
		}
	}*/
	//查询余额
	public static void cha() {
		System.out.println("您当前的余额为:"+money);
	}

}

package cn.sxr.test;
import java.util.Scanner;
public class Test {
	public static void main(String[] args) throws IllegalnumException {
		System.out.println("输入1存钱\n输入2取钱\n输入3查询余额\n输入4退出");
		Bank a = new Bank();
		while(true) {
		System.out.println("请输入您要进行的操作:");
		Scanner sc = new  Scanner(System.in);
		int sc1 = sc.nextInt();
		
		if(sc1==1) {
			
			a.cun();
		}
		else if(sc1==2) {
			try {
				a.qu();
			}
			catch(IllegalnumException e){
				System.out.println(e.getMessage());
				
			}
		}
		else if(sc1==3){
			a.cha();
		}
		else {
			
			break;
		}
			
			
			
	}
		System.out.println("程序结束,感谢使用");
	}

}

4.显示并复制文件

package cn.sxr.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Dem03 {
	
	public static void copyFile(File src,File dest) throws IOException{
		// 在目的地创建文件夹  c:\\Test\\day09\\avi
		File newFile = new File(dest,src.getName());
		// 判断拼接成的路径是否存在
		if(!newFile.exists()){
			newFile.mkdirs();
		}
		// 获取源目录中的所有的文件和文件夹
		File[] files = src.listFiles();
		for (File file : files) {
			if(file.isFile()){
				// 开始复制文件
				FileInputStream fis = new FileInputStream(file);
				// c:\\Test\\day09\\ppt
				FileOutputStream fos = new FileOutputStream(new File(newFile,file.getName()));
				byte[] b = new byte[1024];
				int len;
				while((len  = fis.read(b)) !=-1){
					fos.write(b, 0, len);
				}
				
				fos.close();
				fis.close();
				
			}else if(file.isDirectory()){
				copyFile(file, newFile);
			}
		}
	}

	
	}


package cn.sxr.file;
import java.io.File;
import java.io.IOException;

public class Test {
	public static void main(String[] args) throws IOException {
		 //列出文件
		 String path="f:/myQQ";
		  File file=new File(path);
		  File[] tempList = file.listFiles();
		  System.out.println("该目录下对象个数:"+tempList.length);
		  for (int i = 0; i < tempList.length; i++) {
		   if (tempList[i].isFile()) {
		    System.out.println("文     件:"+tempList[i]);
		   }
		   if (tempList[i].isDirectory()) {
		    System.out.println("文件夹:"+tempList[i]);
		   }
		  }
		
		//复制文件
		Dem03 f = new Dem03();
		// 源目录
		File src = new File("F:\\myQQ");
		// 目的地
		File dest = new File("F:\\aaa\\");
		f.copyFile(src, dest);
	}

}

一些简单实验

package cn.sxr.shiyan;

import java.io.*;
import java.util.*;
public class Chengji{
public static void main(String[] args){
	Scanner input=new Scanner(System.in);
	System.out.println("请输入一个成绩:");
	while(input.hasNext()) {
	int sc = input.nextInt();
	/*if(sc>100||sc<0) {
		System.out.println("输入错误,请重新输入:");
	}*/
	if(sc == 100) {
		System.out.println("A");
	}
	if(sc>=0&&sc<10){
		System.out.println("E");
	}
	switch(sc/10){
	case 9:
		System.out.println("A");
		break;
	case 8:
		System.out.println("B");
		break;
 	case 7:
		System.out.println("C");
		break;
	case 6:
		System.out.println("D");
		break;
	case 5:
	case 4:
	case 3:
	case 2:
	case 1:
		System.out.println("E");
		break;
	default:
		System.out.println("输入错误,请重新输入:");		
	}	

	}
}
}

//圆
package cn.sxr.shiyan;

public class Circle {
	double r;
	public Circle(double r) {
		this.r = r;
	}
	public double getArea() {
		return Math.PI*r*r;
	}
	public double getPerimeter() {
		return 2*Math.PI*r;
	}
	

}


package cn.sxr.shiyan;


public class Test{
	static Circle cr = new Circle(1);
	public static void main(String[] args) {
		double area = cr.getArea();
		double per = cr.getPerimeter();
		System.out.println("面积为:"+area);
		System.out.println("周长为:"+per);
	}
	
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值