1.验证歌德巴赫猜想。一个充分大的偶数(大于或等于6)可以分解为两个素数之和。 试编程序,将 6至50之间全部偶数表示为两个素数之和。
package kkkk;
public class aaa {
public int isprime(int n)/*判断n是否为素数的函数*/ {
int x=0;
for(int j=2;j<n;j++) if(n%j==0) {
x=0; break; }else x=1;
return x;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
aaa b=new aaa();
for(int n=6;n<=50;n+=2) {
for(int i=3;i<=(n/2);i++) {
if(b.isprime(i)!=0&&b.isprime(n-i)!=0)
System.out.println(n+"可以写成"+i+"+"+(n-i));
}
}
}
}
2.两位数13和62具有很有趣的性质:把它们个位数字和十位数字对调,其乘积不变, 即1362=3126。 编程序求共有多少对这种性质的两位数(个位与十位相同的不在此列,如11、22,重复出现的不在此列,如 1362与6213)
package yzp2;
public class fish {
public static void main(String[] args) {
// TODO Auto-generated method stub
int sum=0;
for(int i=11;i<100;i++) {
for(int j=11;j<100;j++) {
if(i%10==0||j%10==0)continue;
if(i%10==i/10||j%10==j/10)continue;
if(i>=j)continue;
if(i*j==(i%10*10+i/10)*(j%10*10+j/10))
{
System.out.println(i+" "+j);sum++;}
}
}
System.out.println("共有:"+sum+"个");
}
}
3.输入1到7之间的某个整数,输出相应的星期:Monday、 Tuesday 等等,用switch语句实现。
package yzp3;
import java.util.Scanner;
public class day {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
int a=input.nextInt();
String today;
switch(a){
case 1:today="monday";break;
case 2:today="tuseday";break;
case 3:today="wednesday";break;
case 4:today="thursday";break;
case 5:today="friday";break;
case 6:today="saturday";break;
case 7:today="sunday";break;
default:today="wrong day";
}
System.out.print(today);
}
}
1.有M个人围成一圈,每人一个的编号(1.2.3…M),从第一个人数起,报到N时这 个人就出圈。再继续数,数到N时这个人又出圈。直到只剩下一个人为止,输出出圈 人的顺序。M、N从键盘输入。
package yzp;
import java.util.Scanner;
public class mongkey {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner A=new Scanner(System.in);
int M=A.nextInt();
int N=A.nextInt();
int a[]=new int[M];
int p=0;
for(int i=0;i<M;i++) {
a[i]=1;
}
for(int j=0;j<M;j++) {
p++; if(a[j]==0)p--;if(p==N)
{
p=0;a[j]=0;System.out.println(j+1); }
if(j==M-1)j=-1;
}
}
}
package how;
public class in {
public static void main(String[] args) {
// TODO Auto-generated method stub
int a[][]=new int[10][10];
cp:for(int i=0;i<10;i++) {
for(int j=0;j<10;j++) {
if(j==0||i==j)a[i][j]=1;
else if(j>i) {
System.out.println();continue cp;}
else a[i][j]=a[i-1][j]+a[i-1][j-1];
System.out.print(a[i][j]+" ");
}
}
}
}
3.编写一个程序打印出有10个元素的数组a中最大值和最小值。
package how;
public class in {
public static void main(String[] args) {
// TODO Auto-generated method stub
int a[]=new int[10];
for(int i=0;i<10;i++) {
a[i]=(int)(Math.random()*10);
System.out.print(a[i]+" ");
}
System.out.println();
int max=a[0];
int min=a[0];
for(int i=1;i<10;i++) {
if(a[i]>max)max=a[i];
if(a[i]<min)min=a[i];
}
System.out.println("max="+max+" min="+min);
}
}
4.输出一个保存在二维数组中3*3矩阵,并求对角线元素之和。
package how;
public class in {
public static void main(String[] args) {
// TODO Auto-generated method stub
int [][]a=new int[3][3];
int sum=0;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++) {
a[i][j]=(int)(Math.random()*10);
System.out.print(a[i][j]+" ");
if(j==2)System.out.println();
if(i==j||i+j==2)sum=sum+a[i][j];
}
System.out.print("sum="+sum);
}
}
5.编写一个模拟的Java发牌程序,要求将两副牌,也就是108张,发给4个人,并留 8张底牌,最后输出底牌和每个人手中的牌。
package fapai;
import java.util.Arrays;
public class lkj {
public static void main(String[] args) {
int[][]a=new int[4][25];
int []n=new int[108];
for(int i=0,j=1;i<108;i=i+2) {
n[i]=j;
n[i+1]=j;
j++;
}
f(a,n,0,0);
}
private static void f(int[][]a, int[] n, int k,int o) {
int i;
for(i=k;i<k+25;i++) {
int m=(int)(Math.random()*(108-i));
a[o][i-k]=n[m];
System.arraycopy(n, m+1, n, m, n.length-m-1);
}
if(o<3)f(a,n,i,o+1);
else {
for(int j=0;j<4;j++) {
System.out.println("第"+(j+1)+"人手里的牌为:"+Arrays.toString(a[j]));
}
for(int j=0;j<8;j++) {
if(j==0)System.out.print("剩余的8张底牌为:"+"[");
else if(j<7)System.out.print(n[j]+", ");
else System.out.printf("%d]",n[j]);
}
}
}
}
package pen;
public class AutoPencil {
private boolean penPoint;
public AutoPencil() {
penPoint=false;
}
public void knock() {
penPoint=!penPoint;
}
public Boolean getPenPoint() {
return penPoint;
}
public static void main(String[] args) {
AutoPencil atPen=new AutoPencil();
System.out.println(atPen.getPenPoint());
atPen.knock();
System.out.println(atPen.getPenPoint());
}
}
2.设计一个汽车类Car,有2个属性车主owner和油量oil,有7个函数:
(1)构造函数Car(String owner,double oil)
(2)构造函数Car(String owner),油量置为0
(3)加油操作函数void addOil(double oil)
(4)行驶操作函数void drive(int distance)
假设每100公里的耗油量为定值(自己设置),根据行驶的公里数更新油量。
(5)获得车主信息函数String getOwner()
(6)获得油量函数double getOil()
(7)main函数
创建对象并对实例对象进行加油、行驶操作,显示车主、油量。
package knigt;
public class Car {
private String owner;
private double oil;
public Car(String owner,double oil){
this.owner=owner;
this.oil=oil;
}
public Car(String owner){
this.owner=owner;
oil=0;
}
public void addOil(double oil) {
this.oil=this.oil+oil;
}
public void drive(int distance) {
oil=oil-15*distance;
}
public String getOwner() {
return owner;
}
public double getOil() {
return oil;
}
public static void main(String[] args) {
Car ok=new Car("张帅",10);
Car in=new Car("卢泽龙");
System.out.println(ok.getOwner());
System.out.println(ok.getOil());
System.out.println(in.getOwner());
System.out.println(in.getOil());
in.addOil(60);
System.out.println(in.getOwner()+"加油后有:"+in.getOil());
in.drive(2);
System.out.println(in.getOwner()+"行驶后有:"+in.getOil());
}
}
3.编写Java程序模拟简单的计算器。 定义名为Number的类,其中有两个私有的整型数据成员x和y。有7个函数:
(1)构造函数Number(int x,int y)
(2)加法运算函数 int addition(int x,int y)
(3)减法运算函数 int subtration(int x,int y)
(4)乘法运算函数 int multiplication(int x,int y)
(5)除法运算函数 int division(int x,int y)
另外编写一个测试类,对Number类进行测试。
package pen;
class Number{
private int x,y;
Number(int x,int y){
this.x=x;
this.y=y;
}
public int addition(int x,int y) {
return x+y;
}
public int subtration(int x,int y) {
return x-y;
}
public int multiplication(int x,int y) {
return x*y;
}
public int division(int x,int y) {
return x/y;
}
}
public class shixian {
public static void main(String[] args) {
Number A=new Number(8,2);
System.out.println(A.addition(8,2));
System.out.println(A.subtration(8,2));
System.out.println(A.multiplication(8,2));
System.out.println(A.division(8,2));
}
}
package pen;
class Rectangle{
int width;
int height;
Rectangle(){
width=1;
height=1;
}
Rectangle(int val){
width=val;
height=val;
}
Double getSquare() {
return (double) (width*height);
}
Double getPerimeter() {
return (double)(2*width+2*height);
}
}
public class shixian {
public static void main(String[] args) {
Rectangle A=new Rectangle(6);
System.out.println(A.getSquare());
System.out.println(A.getPerimeter());
}
}
package pen;
class MyTime{
private int hour,minute,second;
MyTime(int x,int y,int z){
hour=x;
minute=y;
second=z;
}
public void nextSecond() {
if(second!=59)second+=1;
else
{
second=0;if(minute!=59)minute+=1;
else {
minute=0;if(hour!=23)hour+=1;
else hour=0;
}
}
}
public void nextMinute() {
if(minute!=59)minute+=1;
else {
minute=0;if(hour!=23)hour+=1;
else hour=0;
}
}
public void nextHour() {
if(hour!=23)hour+=1;
else hour=0;
}
public void preSecond() {
if(second!=0)second-=1;
else
{
second=59;if(minute!=0)minute-=1;
else
{
minute=59;if(hour!=0)hour-=1;
else hour=23;
}
}
}
public void prevMinute() {
if(minute!=0)minute-=1;
else
{
minute=59;if(hour!=0)hour-=1;
else hour=23;
}
}
public void prevHour() {
if(hour!=0)hour-=1;
else hour=23;
}
public void display(){
System.out.println(hour+":"+minute+":"+second);
}
}
public class shixian {
public static void main(String[] args) {
MyTime A=new MyTime(22,0,59);
A.prevMinute();
A.display();
A.nextSecond();
A.display();
}
}
6.编写程序使用复数类Complex验证两个复数 2+2i 和3+3i 相加产生一个新的复数5+5i 。复数类Complex满足如下要求:
(1)属性
int realPart :复数的实部;
int imaginPart:复数的虚部;
(2)方法
Complex( ) : 构造方法,将复数的实部和虚部都置0;
Complex( int r , int i ) :
Complex complexAdd(Complex a) : 将当前复数对象与形参复数对象相加,所得的结 果仍是一个复数值,返回给此方法的调用者。
String toString( ) : 把当前复数对象的实部、虚部组合成 a+bi 的字符串形式,其中 a 和 b分别为实部和虚部的数据。
另外编写一个测试类Test,对Complex类进行测试。
package pen;
class Complex{
int realPart;
int imaginPart;
Complex(){
realPart=0;
imaginPart=0;
}
Complex(int r,int i){
realPart=r;
imaginPart=i;
}
Complex complexAdd(Complex a) {
realPart=realPart+a.realPart;
imaginPart=imaginPart+a.imaginPart;
return a;
}
public String toString() {
return realPart+"+"+imaginPart+"i";
}
}
public class Test {
public static void main(String[] args) {
Complex A=new Complex(1,2);
Complex B=new Complex(4,6);
A.complexAdd(B);
System.out.println(A.toString());
}
}
实验目的
在App类的main()中,创建一个Compute类对象、一个Circle类对象和一个Rectangle类对象,用Circle类的area方法计算圆的面积,用Rectangle类的area方法计算长方形的面积,用Compute类的compute方法计算圆、长方形的面积。
package Ko;
interface shape{
double pi=3.14;
double area();
}
class compute{
public double computeArea(shape s) {
return s.area();
}
}
class circle implements shape{
int radius;
circle(int radius){
this.radius=radius;
}
public double area() {
return radius*radius*pi;
}
}
class Rectangle implements shape{
int width;
int hight;
Rectangle(int width,int hight){
this.width=width;
this.hight=hight;
}
public double area() {
return width*hight;
}
}
public class App {
public static void main(String[] args) {
compute A=new compute();
circle B=new circle(3);
shape C=new Rectangle(6,4);
System.out.println("Rectangle area:"+C.area());
System.out.println("circle area:"+A.computeArea(B));
System.out.println("circle area:"+B.area());
}
}
package code1;
import java.util.Scanner;
abstract class AsciiArt{
String title;
String author;
void AsciiArt(String title,String author) {
this.title=title;
this.author=author;
}
abstract public void draw();
String getTitle() {
return title;
}
void setTitle(String title) {
this.title=title;
}
String getAuthor() {
return author;
}
void setAuthor(String author) {
this.author=author;
}
}
class Paint{
AsciiArt art;
void setArt(AsciiArt art) {
this.art=art;
}
void draw() {
System.out.println("作品:"+art.title);
System.out.println("作者:"+art.author);
art.draw();
}
}
class Circle extends AsciiArt{
int radius;
Circle(String title,String author){
this.title=title;
this.author=author;
}
public static int getx(int r,int y)
{
return (int)Math.round(r-Math.sqrt(r*r-(r-y)*(r-y)));
}
public static String getS(int s) {
String space="";
for(int i=0;i<s;i++)
{
space+=" ";
}
return space;
}
public void draw() {
int x=0,y=0,r=0;
System.out.print("请输入半径:");
Scanner input=new Scanner(System.in);
r=input.nextInt();
for(int i=0;i<=2*r;i+=2) {
x=getx(r,y);
System.out.print(getS(x));
System.out.print("*");
System.out.print(getS(2*(r-x)));
System.out.print("*");
System.out.print('\n');
y+=2;
}
}
}
class Rectangle extends AsciiArt{
int width;
int height;
Rectangle(int width,int height){
this.width=width;
this.height=height;
}
public void draw() {
for(int i=0;i<height;i++) {
for(int j=0;j<width;j++) {
if(i==0||i==height-1||j==0||j==width-1)
System.out.print("*");
else System.out.print(" ");
}
System.out.println();
}
}
}
class Rabit extends AsciiArt{
Rabit(){
title="Rabit";
author="luzelong";
}
public void draw() {
System.out.println("/)/)");
System.out.println("(- -) )o");
System.out.println(" || ||");
}
}
public class App {
public static void main(String[] args) {
int p;
AsciiArt A=new Rectangle(5,10);
AsciiArt B=new Rabit();
AsciiArt C=new Circle("aaa1","aaa2");
A.setTitle("bbb1");
A.setAuthor("bbb2");
Paint a=new Paint();
a.setArt(A);
Paint b=new Paint();
b.setArt(B);
Paint c=new Paint();
c.setArt(C);
Paint paint=new Paint();
System.out.println("--------------------");
System.out.println("1.圆形");
System.out.println("2.矩形");
System.out.println("3.小兔子");
System.out.println("--------------------");
System.out.print("请选择:");
Scanner io=new Scanner(System.in);
p=io.nextInt();
switch(p) {
case 1:c.draw();break;
case 2:a.draw();break;
case 3:b.draw();break;
default:System.out.println("输入有误!");
}
}
}
实验目的
1.了解Java中一些常用的基本类库的结构和主要功能。
2.掌握Java中Integer类、String类、StringBuffer类的使用方法。
实验学时 2学时
实验内容
package sixoe;
import java.util.Scanner;
public class Cout {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int sum=0;
System.out.print("请输入第一个字符串:");
String str1=input.nextLine();
System.out.print("请输入第二个字符串:");
String str2=input.nextLine();
int lo=str1.indexOf(str2);
while(lo!=-1) {
sum++;
lo=str1.indexOf(str2,lo+1);
}
System.out.println("第二个字符串在第一个字符串中出现了"+sum+"次!");
}
}
2.输入一行字符串(只有26个字母构成),统计有多少个字母(不区分大小写)及每 个字母出现的个数。
package sixtwo;
import java.util.Scanner;
public class TJ {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
String str=input.nextLine();
StringBuffer k=new StringBuffer(str);
int length=k.length();
System.out.println("共有"+length+"个字母!");
String a;int index,sum;
for(int i=0;i<k.length();) {
sum=0;
a=k.substring(0,1);
index=k.indexOf(a);
while(index!=-1) {
sum++;
k.deleteCharAt(index);
index=k.indexOf(a,index);
}
System.out.println(a+"字母有"+sum+"个");
}
}
}
3.输入一个数字串,先将它转换为整数,再分别以2进制、8进制、16进制输出。
package six.three;
import java.util.Scanner;
public class kkk {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
System.out.println("请输入一个数字字符串:");
String a=input.nextLine();
int b=Integer.parseInt(a);
System.out.println("二进制为:"+Integer.toBinaryString(b));
System.out.println("八进制为:"+Integer.toOctalString(b));
System.out.println("十六进制为:"+Integer.toHexString(b));
}
}
4.输入包含一个非负整数a(0<=a<=2147483647),将a转换成16进制并输出。
package six.four;
import java.util.Scanner;
public class uj {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
int a=input.nextInt();
System.out.println("转化为十六进制为:"+Integer.toHexString(a));
}
}
5.输入一个十六进制数,输出对应的八进制数。
package six.sevn;
import java.util.Scanner;
public class lkhj {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.print("请输入一个十六进制数字");
Scanner input=new Scanner(System.in);
String six=input.nextLine();
int a=Integer.parseInt(six,16);
System.out.println(Integer.toOctalString(a));
}
}
实验目的
1.熟悉Java中异常类库的结构和主要功能。
2.掌握Java中异常处理机制、自定义异常。
实验学时 2学时
实验内容
package play;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Out {
public void input() {
Scanner s=new Scanner(System.in);
System.out.println("请输入一个正整数:");
try {
int a=s.nextInt();
if(a<0)throw new InputMismatchException();
System.out.println("输入正确!");
}catch (InputMismatchException e) {
System.out.println("输入的不是正整数,请重新输入:");
input();
}
}
public static void main(String[] args) {
Out t=new Out();
t.input();
}
}
实验目的
package IO;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Copy {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
int size;
FileInputStream f=new FileInputStream("src/IO/Copy.java");
FileOutputStream fout=new FileOutputStream("copy-of-file.txt");
size=f.available();
int n=size/10;
byte b[]=new byte[n];
int count=0;
while((count=f.read(b,0,n))!=-1)
fout.write(b,0,count);
System.out.println("拷贝成功!");
f.close();
fout.flush();
fout.close();
}
}
方法2:
package IO;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Copjy2 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
File file=new File("src/IO/Copjy2.java");
FileReader f=new FileReader(file);
FileWriter fout=new FileWriter("copy-of-file2.txt");
int n=(int)(file.length()/10);
char b[]=new char[n];
int count=0;
while((count=f.read(b,0,n))!=-1)
fout.write(b,0,count);
System.out.println("拷贝成功!");
f.close();
fout.close();
}
}
题2的代码如下
package IO;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class FileCutMerge {
public static void main(String[] args) {
FileCutMerge tool=new FileCutMerge();
if((args==null)||(args.length!=2)) {
tool.help();
}else if(args[0].startsWith("-c")) {
File f1=new File(args[1]);
if(!f1.exists()) {
System.out.println("指定的文件不存在");
}
int fileSize=Integer.parseInt(args[0].substring(2));
try {
tool.cut(f1,fileSize);
}catch(Exception e) {
e.printStackTrace();
}
}else if(args[0].equals("-m")) {
String prefixname=args[1];
File f=new File(".");
File[] names=f.listFiles(new MyFilesFilter(prefixname));
try {
tool.merge(names);
}catch(Exception e) {
e.printStackTrace();
}
}else {
}
}
public void help() {
System.out.println("错误的命令行格式,正确的是:");
System.out.println("FileCutMerge-c filename");
System.out.println("或者");
System.out.println("FileCutMerge-m filenameprefix");
}
public void cut(File file,int size)throws Exception{
System.out.println("开始分割文件....");
File parent=file.getParentFile();
long fileLength=file.length();
int filenum=(int)(fileLength/size);
if(fileLength%size!=0) {
filenum+=1;
}
String[] smallfilenames=new String[filenum];
FileInputStream fin=new FileInputStream(file);
byte []buf=new byte[size];
for(int i=0;i<filenum;i++) {
File outfile=new File(parent,file.getName()+"-"+i);
FileOutputStream fout=new FileOutputStream(outfile);
int cout=fin.read(buf);
fout.write(buf,0,cout);
fout.close();
smallfilenames[i]=outfile.getName();
}
fin.close();
System.out.println("分割后的文件如下:");
for(int i=0;i<smallfilenames.length;i++) {
System.out.println(smallfilenames[i]);
}
System.out.println("文件分割完成");
}
public void merge(File[] files)throws Exception{
System.out.println("开始合并文件...");
String smallfilename=files[0].getName();
int pos=smallfilename.indexOf("-");
String tagetfilename="new-"+smallfilename.substring(0,pos);
System.out.println("合并后的文件为:"+tagetfilename);
File outFile=new File(files[0].getParentFile(),tagetfilename);
FileOutputStream fout=new FileOutputStream(outFile);
for(int i=0;i<files.length;i++) {
FileInputStream fin=new FileInputStream(files[i]);
int b;
while ((b=fin.read())!=-1) {
fout.write(b);
}
fin.close();
}
fout.close();
System.out.println("合并文件完成。");
}
}
class MyFilesFilter implements FileFilter{
String prefix="";
public MyFilesFilter(String prefix) {
this.prefix=prefix;
}
@Override
public boolean accept(File f) {
if(f.getName().length()>prefix.length()&&f.getName().startsWith(prefix)) {
return true;
}
return false;
}
}