栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

期末考试编程题目

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

期末考试编程题目

书本38面T3,T5

//秋水花数
public class Test1 {
		static int sum = 0;
		
		public static void main(String[] args) {
			for(int i =100 ; i < 1000 ; i++)
			{
				int a = i/100 ;       //a表示i的百位数值 ;
				int b = (i/10)%10 ;   //b表示i的十位数值 ;
				int c = i % 10;			//c表示i的个位数值 ;
				if(a*a*a + b*b*b + c*c*c == i)
				{
					System.out.println(i);
				}
			}
		}
}

//计算1!+2!+...+100!:

public class Test1{
static int sum = 0;

public static void main(String[] args) {
	double sum = 0,i = 1,j = 0 ,k = 1;

		do{
			k = 1;
			j = i;
				do{
					k = k * j;
					j--;
				}while(j >= 1);
			sum = sum + k;
			i++;
		}while(i <= 100);
		
		System.out.println("1!+2!+...+100! = "+sum);
	}
}

 

书本第60面T1,T2 

//complex类:

public class complex {
	int realpart;
	int imaginpart;

	complex() {
		realpart = 0;
		imaginpart = 0;
	}

	complex(int r, int i) {
		realpart = r;
		imaginpart = i;
	}

	complex complexAdd(complex com) {

		return new complex(this.realpart + com.realpart, this.imaginpart + com.imaginpart);

	}
}

//Test测试类:
public class Test1 {

	public static void main(String[] args) {
		complex c[] = new complex[5];
		c[1] = new complex(1, 2);
		c[2] = new complex(1, -2);
		c[3] = new complex(3, 4);
		c[4] = new complex(-1, 2);
		for (int i = 2; i <= 4; i++) {
			c[0] = c[1].complexAdd(c[i]);
			System.out.println("(" + c[1].realpart + (c[1].imaginpart < 0 ? "" : "+") 
					+ c[1].imaginpart + "i)+("
					+ c[i].realpart + (c[i].imaginpart < 0 ? "" : "+") 
					+ c[i].imaginpart + "i)="+ (c[0].realpart == 0 ? (c[0].imaginpart == 0 ? 0 : "") : c[0].realpart)
					+ (c[0].imaginpart == 0 ? "" : ((c[0].imaginpart < 0 ? "" : "+") + c[0].imaginpart + "i")));
		}

	}

}
//Rectangle类:

public class Rectangle {
	double width;
	double length;

	Rectangle(double l, double w) {
		check(w, l);
		width = w;
		length = l;
	}

	public void check(double w, double l) {
		if (w <= 0 || w > 50 || l <= 0 || l > 50) {
			System.out.print("长或宽超出规定范围!");
			System.exit(0);
		}
	}

	public double getArea() {
		return this.length * this.width;
	}

	public double getPerimeter() {
		return (this.length + this.width) * 2;
	}

	public void Draw() {
		for (int i = 1; i <= this.width; i++) {
			for (int j = 1; j <= this.length; j++) {
				if (i == 1 || i == this.width) {
					System.out.print("*");
				} else {
					if (j == 1 || j == this.length) {
						System.out.print("*");
					} else {
						System.out.print(" ");
					}
				}

			}
			System.out.print("n");
		}
	}
}

// Test测试类:

public class Test1 {

	public static void main(String[] args) {
		Rectangle rec = new Rectangle(10, 5);
		System.out.println("面积:" + rec.getArea());
		System.out.println("周长:" + rec.getPerimeter());
		rec.Draw();
	}

}

课本90,T1,T2 

//ShapArea接口:

interface ShapArea {
	double getArea();

	double getPerimeter();

}

// MyRectangle类:

public class MyRectangle implements ShapArea {

	double width;
	double height;

	public MyRectangle(double w, double h) {
		width = w;
		height = h;

	}

	
	public double getArea() {

		return width * height;
	}


	public double getPerimeter() {

		return (width + height) * 2;
	}

	public String toString() {
		System.out.println("width = " + width + " , height = " + height + " , perimeter = " + getPerimeter()
				+ " ,  area = " + getArea());

		return "width = " + width + " , height = " + height + " , perimeter = " + getPerimeter() + " ,  area = "
				+ getArea();
	}

}

// MyTriangle类:

public class MyTriangle implements ShapArea {

	double a, b, c, p;

	public MyTriangle(double x, double y, double z) {
		a = x;
		b = y;
		c = z;
		p = (a + b + c) / 2;
	}


	public double getArea() {

		return Math.sqrt(p * (p - a) * (p - b) * (p - c));
	}


	public double getPerimeter() {

		return a + b + c;
	}

	public String toString() {
		System.out.println("three sides:" + a + " , " + b + " , " + c + " , perimeter =" + getPerimeter() + " , area = "
				+ getArea());

		return "three sides:" + a + " , " + b + " , " + c + " , perimeter =" + getPerimeter() + " , area = "
				+ getArea();
	}

}

// Test测试类:

public class Test1 {

	public static void main(String[] args) {
		MyRectangle m = new MyRectangle(5, 6);
		m.toString();
		MyTriangle t = new MyTriangle(3, 4, 5);
		t.toString();
	}

}
//Area接口
public interface Area {

	double CalsulateArea();

}

//MyCircle类
public class MyCircle implements Area {

	double radius;
	final double PI = 3.1415926;

	public MyCircle(double r) {
		radius = r;
	}

	public double CalsulateArea() {

		return PI * radius * radius;
	}

}

//MyRectangle类
public class MyRectangle implements Area {

	double width, height;

	public MyRectangle(double w, double h) {

		width = w;
		height = h;
	}

	public double CalsulateArea() {

		return width * height;
	}

}

//Test测试类
public class Test {

	public static void main(String[] args) {
		MyRectangle m = new MyRectangle(5, 6);
		MyCircle m1 = new MyCircle(10);
		System.out.println("正方形m的面积:" + m.CalsulateArea() + "n圆形m1的面积:" + m1.CalsulateArea());

	}

}

课本183 T2 T4

//第2题

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class Frame2 extends JFrame implements ActionListener {
	JButton btn1, btn2;
	JTextArea txt;

	Frame2() {
		this.setBounds(10, 10, 600, 400);
		this.setLayout(new FlowLayout(FlowLayout.CENTER));
		btn1 = new JButton("按钮btn1");
		btn1.addActionListener(this);
		btn2 = new JButton("按钮btn2");
		btn2.addActionListener(this);
		txt = new JTextArea(1, 15);
		this.add(txt);
		this.add(btn1);
		this.add(btn2);

		this.setVisible(true);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
	}

	public static void main(String args[]) {
		new Frame2();
	}

	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if (e.getSource() == btn1) {
			txt.setText("按钮btn1");
		}
		if (e.getSource() == btn2) {
			txt.setText("按钮btn2");
		}

	}

}
public class Point{

	double x ,y;
	public Point(double x , double y) {
		this.x = x;
		this.y = y;
	}
	
	double getX(){
		return x;
	}
	double getY(){
		return y;
	}
	void setX(double x){
		this.x = x;
	}
	void setY(double y){
		this.y = y;
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Point p = new Point(10,20);
		System.out.println("坐标: ("+p.getX()+","+p.getY()+")");
		p.setX(100);
		System.out.println("坐标: ("+p.getX()+","+p.getY()+")");
		p.setY(200);
		System.out.println("坐标: ("+p.getX()+","+p.getY()+")");
	}

}

课本207 T3,T4 

import java.io.*;
import java.util.*;

public class Test_3 {
	public static void main(String[] args) throws Exception {
		String fileName = "txt.txt";
		System.out.println("输入一个文件名");
		Reader is = new InputStreamReader(System.in);
		fileName = (new BufferedReader(is)).readLine();
		FileInputStream fis = new FileInputStream(fileName);
		int ch;		
		Map ha = new HashMap();
		
		for(int i = 'a'; i <= 'z'; i++){
				ha.put((char)i,0);
		}
		
		while((ch = fis.read()) != -1){
				if(((char)ch >= 'a' && (char)ch <= 'z') || ((char)ch >= 'A' && (char)ch <= 'Z')){
							if((char)ch >= 'A' && (char)ch <= 'Z'){
								ch = ch + 32;
							}
							int j = 0;
							j = ha.get((char)ch);
							ha.put((char)ch,j+1);			
				}
		}
		
		for(int k = 'a'; k <= 'z'; k++){
				if(ha.get((char)k) != 0){
					System.out.println((char)k + " = " + ha.get((char)k));
				}
		}
		fis.close();
	}
}
import java.util.*;
	  
	public class Test_4 {  
	public static void main(String arg[]){  
	    int wordCount=0;      //用于统计单词的总个数  
	    Map map=new HashMap();//用于统计各个单词的个数,排序  
    	String sentence="hello,my name is Tom,what is your name?he said:"my name is John"";  
	   StringTokenizer token=new StringTokenizer(sentence); 
	    while(token.hasMoreTokens()){                      //循环遍历  
	        wordCount++;    
	                                        
	        String word=token.nextToken(", ?.!:""''n");  //括号里的字符的含义是说按照,空格 ? . : "" '' n去分割,如果这里你没明确要求,即括号里为空,则默认按照空格,制表符,新行符和回车符去分割  
	        if(map.containsKey(word)){     //HashMap不允许重复的key,所以利用这个特性,去统计单词的个数  
	            int count=map.get(word);  
            map.put(word, count+1);     //如果HashMap已有这个单词,则设置它的数量加1  
	        }  
	        else  
	            map.put(word, 1);          //如果没有这个单词,则新填入,数量为1  
	    }  
	    System.out.println("总共单词数:"+wordCount);  
	    sort(map);                        //调用排序的方法,排序并输出!  
	}  
	public static void sort(Map map){  
	    List> infoIds = new ArrayList>(map.entrySet());   
	    Collections.sort(infoIds, new Comparator>() {     
       public int compare(Map.Entry o1, Map.Entry o2) {     
            return (o2.getValue() - o1.getValue());     
	        }     
	}); //排序  
	    for (int i = 0; i < infoIds.size(); i++) {   //输出  
	        Map.Entry id = infoIds.get(i);  
	    System.out.println(id.getKey()+":"+id.getValue());  
	        }  
	}  
	}  

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/978526.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号