zoukankan      html  css  js  c++  java
  • 《Java技术》第二次作业

    (一)学习总结

    1.什么是构造方法?什么是构造方法的重载?下面的程序是否可以通过编译?为什么?

    public class Test {
        public static void main(String args[]) { 
           Foo obj = new Foo();       
        }     
    }
    class Foo{
        int value;
        public Foo(int intValue){
            value = intValue;
        }
    }
    

    构造方法是一个与类同名且没有返回值类型的方法。其功能主要是创建对象时完成对象的初始化。用户不能直接调用,而是通过关键字new自动调用。 构造方法和其他方法一样也可以重载。 方法的重载就是方法名称相同,但参数的类型和参数的个数不同,通过传递参数的个数及类型不同以完成不同功能的方法调用。
    不能,用构造方法的时候 那个class类是有参数的,而调用的时候 是没有的。
    2.运行下列程序,结果是什么?分析原因,应如何修改。

    public class Test {
        public static void main(String[] args) {
            MyClass[] arr=new MyClass[3];
            arr[1].value=100;
        }
    }
    class MyClass{
        public int value=1;
    }
    

    有错,没有实例化(改后)

    public class A {
        public static void main(String[] args) {
            MyClass arr=new MyClass();
            arr.value =100;
            System.out.println(arr.value);
        }
    }
    

    3.运行下列程序,结果是什么?说明原因

    public class Test {
        public static void main(String[] args) {
            Foo obj1 = new Foo();
            Foo obj2 = new Foo();
            System.out.println(obj1 == obj2);
        }
    }
    class Foo{
        int value = 100;
    }
    

    false 原因 地址不相等==比较的是地址(改后)

    public class A {
        public static void main(String[] args) {
            Foo obj1 = new Foo();
            Foo obj2 = new Foo();
            System.out.println(obj1.value == obj2.value);
        }
    }
    class Foo{
        int value = 100;
    }
    

    4.什么是面向对象的封装性,Java中是如何实现封装性的?试举例说明。
    ,封装性就是把对象的成员属性和成员方法结合成一个密不可分的整体,将两者“封装”一个不可分割的独立单位(对象)
    并尽可能隐蔽对象的内部细节,把不需要让外界知道的信息隐藏起来,有些对象的属性及行为允许外界用户知道或使用,
    但不允许更改,而另一些属性或行为,则不允许外界知道,或只允许使用对象的功能,尽可能隐藏对象的功能,实现细节。
    属性封装:private 属性类型 属性名称;
    方法封装:private 方法返回值 方法名称{}

    (运行结果)
    5.阅读下面程序,分析是否能编译通过?如果不能,说明原因。
    (1)

    class A{
        private int secret = 5;
    }
    public class Test{
        public static void main(String args[]){
            A a = new A();
            System.out.println(a.secret++);
        }
    }
    

    不能 secret是私有的 只能在自己的类里使用

    (2)

    public class Test{
        int x = 50;
        static int y = 200;
        public static void method(){
            System.out.println(x+y);
        }
        public static void main(String args[]){
            Test.method();
        }
    }
    

    不能 因为x定义的是int的 但是你的方法是static型的
    6.使用类的静态变量和构造方法,可以跟踪某个类创建的对象个数。声明一个图书类,数据成员为编号,书名,书价,并拥有静态数据成员册数记录图书的总数。图书编号从1000开始,每产生一个对象,则编号自动递增(利用静态变量和构造方法实现)。下面给出测试类代码和Book类的部分代码,将代码补充完整。

    class Book{
        int bookId;
        String bookName;
        double price;
        static int id=1000,num=0;// 声明静态变量,对静态变量初始化
        public Book(String bookName,double price) {//构造方法
        	id++;//只要有对象产生 编号就加一
        	this.bookName=bookName;
        	this.price=price;
        	this.bookId=id;
        	num++;
        }
         public String getBookName() {
            return bookName;
        }
        public void setBookName(String bookName) {
            this.bookName = bookName;
        }
        public double getPrice() {
            return price;
        }
        public void setPrice(double price) {
            this.price = price;
        }  
        public static int totalBook() {//定义方法求图书总册数
        	return num;
        }
        public String toString() {//重写toString方法
        	return "编号:"+bookId+"	书名: "+this.getBookName()+"	价格:"+this.getPrice();
        }
    }
    public class A{
        public static void main(String args[]){ 
            Book[] books = {new Book("c语言程序设计",29.3),
                            new Book("数据库原理",30),
                            new Book("Java学习笔记",68)};
            System.out.println("图书总数为:"+ Book.totalBook()); 
            for(Book book:books){
                System.out.println(book.toString());
            }
        }   
    }
    

    7.什么是单例设计模式?它具有什么特点?用单例设计模式设计一个太阳类Sun。
    单例模式 是在设计一个类时,需要保证在整个程序运行期间针对该类只存在一个实例对象。
    特点: 1、单例类只能有一个实例。
       2、单例类必须自己创建自己的唯一实例。
       3、单例类必须给所有其他对象提供这一实例。

    class Sun{
    	private static Sun instance = new Sun() ;
    	private Sun(){   }
    	public static Sun getInstance(){
    		return instance ;
    	}
    }
    

    8..理解Java参数传递机制,阅读下面的程序,运行结果是什么?说明理由.

    public class Test {
        String str = new String("你好  ");
        char[] ch = { 'w','o','l','l','d' };
        public static void main(String args[]) {
            Test test = new Test();
            test.change(test.str, test.ch);
            System.out.print(test.str);
            System.out.print(test.ch);
        }
        public void change(String str, char ch[]) {
            str = "hello";
            ch[0] = 'W';
        }
    }
    

    运行结果 你好 Wolld 因为JAVA中传参 传的是副本,在change方法里 改的是str的副本 而string本身是final类型的 改不了长度 所以输出结果是不会变的。

    (二)实验总结

    1.用面向对象思想完成评分系统

    package pfxt;//选手类
    public class Person implements Comparable<Person>{
    	private String name;		//姓名
    	private String num;			//编号
    	private double score;		//最终得分
    	public Person(){};
    	public Person(String name,String num,double score){
    		this.name=name;
    		this.num=num;
    		this.score=score;
    	}
    	public String getname() {
    		return name;
    	}
    	public void setname(String n) {
    		name=n;
    	}
    	public String getnum() {
    		return num;
    	}
    	public void setnum(String a) {
    		num=a;
    	}
    	public double getscore() {
    		return score;
    	}
    	public void setscore(double n) {
    		score=n;
    	}
    	public String toString(){
    		return "编号:"+this.getnum()+"	姓名:"+this.getname()+"	最终得分:"+(float)this.getscore();
    	}
    	
    	public int compareTo(Person stu) { 
    	         if (this.score > stu.score) { 
    	               return -1;   
    	         } else if (this.score < stu.score) { 
    	            return 1; 
    	       } else { 
    	            return 0; 
    	      } 
    	   } 
    }
    
    package pfxt;//评分类
    import java.util.*;
    public class Score {
    	private int peo;		//评委人数
        double[] score=null;		//分数
        Scanner input=new Scanner(System.in);
    	public Score(){};
    	public Score(int peo){
    		this.peo=peo;
    	}
    	public int getpeo() {
    		return peo;
    	}
    	public void setpeo(int a) {
    		peo=a;
    	}
    	
    	public void inputScore() {
    		double[] sco1=new double[this.peo];
    		for(int i=0;i<this.peo;i++){
    			sco1[i]=input.nextDouble();
    		}
    		score=sco1;
    	}
        public double average() {
    		double max,min,ave,sum=0;
    		max=score[0];
    		min=score[0];
    		for(int i=0;i<score.length;i++){
    			if(max<score[i]){
    				max=score[i];
    			}
    			if(min>score[i]){
    				min=score[i];
    			}
    			sum=sum+score[i];
    		}
    		sum=sum-max-min;
    		System.out.println("去掉一个最高分: "+max);
    		System.out.println("去掉一个最低分: "+min);
    		ave=sum/(this.peo-2);
    		return ave;
    	}
    }
    
    package pfxt;//测试类
    import java.util.*;
    import java.util.Arrays; 
    public class Text {
    	public static void main(String[] args){
    		Scanner input=new Scanner(System.in);
    		String cno,cna;
    		double ave;
    		System.out.println("输入选手数:");
    		int xuan=input.nextInt();
    		System.out.println("输入评委数:");
    		int ping=input.nextInt();
    		Person []student=new Person[xuan];
    		for(int i=0;i<xuan;i++){
    			System.out.println("输入选手编号:");
    			cno=input.next();
    			System.out.println("输入选手姓名:");
    			cna=input.next();
    			System.out.println("请为"+cno+"号选手打分");
    			Score player=new Score(ping);
    			player.inputScore();
    			ave=player.average();
    			System.out.println(cno+"号选手最后得分:"+(float)ave);
    			student [i]=new Person(cno,cna,ave);
    		}
    		System.out.println("排行榜");
    		
    		Arrays.sort(student);
    		System.out.println("en···");
    		for(int i=0;i<xuan;i++){
    			System.out.println(student [i].toString());
    		}
    	}
    }
    

    2.Email验证

    import java.util.*;
    public class E {
    	public static void main(String[] args) {
    		Scanner input=new Scanner(System.in);
    		String add;
    		System.out.println("请输入一个email地址:");
    		add=input.next();
    		System.out.println(chuan(add));
    	}
    	public static boolean chuan(String ad) {
    		int a,b;
    		a=ad.indexOf("@");
    		b=ad.indexOf(".");
    		if(a!=-1&&b!=-1) {
    			if(a<b) {
    				if(ad.startsWith("@"))
    					return false;
    				else {
    					if(ad.endsWith("com")||ad.endsWith("cn")||ad.endsWith("net")||ad.endsWith("gov")||ad.endsWith("edu")||ad.endsWith("org")) {
    						return true;
    					}
    					else{
    						return false;
    					}
    				}
    			}
    			else {
    				return false;
    			}
    		}
    		return false;
    	}
    }
    
    

    3.查找子串

    import java.util.*;
    public class C {
    	public static void main(String[] args) {
    	Scanner input=new Scanner(System.in);
    	String cou,n;
    	int a;
    	System.out.println("请输入字符串:");
    	cou=input.next();
    	System.out.println("请输入要查找的子串:");
    	n=input.next();
    	a=count(cou,n);
    	System.out.println("出现的次数为:"+a);
    	}
    	public static int count(String x,String y) {
    		int a,num=0,b;
    		b=y.length();
    		a=x.indexOf(y);
    		while(a!=-1) {
    			a=x.indexOf(y, a+b);
    			num++;
    		   }
    		return num;
    	}
    	
    }
    
    

    4.统计文件

    import java.util.*;
    public class S {
    	public static void main(String[] args) {
    		Scanner input=new Scanner(System.in);
    		String s;
    		System.out.println("请输入一串文件名,文件之间用逗号分隔:");
    		s=input.next();
    		sta(s);
    	}
    	public static void sta(String a) {
    		int x;
    		String str[]=a.split(",");
    		String b[]=new String[str.length];
    		int num[]=new int[str.length];
    		for(int i=0;i<str.length;i++) {
    			str[i]=str[i].substring(0, 1).toUpperCase()+str[i].substring(1);
    			System.out.println(str[i]);
    			x=str[i].lastIndexOf(".");
    			b[i]=str[i].substring(x+1);
    		}
    		for(int i=0;i<b.length;i++) {
    			if(b[i].equals("1")== false)
    			{num[i]++;}
    			for(int j=i+1;j<b.length;j++) {
    				if(b[i].equals(b[j])) {
    					b[j]="1";
    					num[i]++;
    				}
    			}
    		}
    		for(int i=0;i<b.length;i++) {
    			if(b[i].equals("1")==false) {
    				System.out.println("与"+b[i]+"相同文件的个数为"+num[i]);
    			}
    		}
    			
    	}
    }
    //a.txt,b.doc,c.txt,d.java,e.java,f.g.txt
    

    5.类的设计

    public class Data{//日期类
    	private int year;
    	private int month;
    	private int data;
    	public Data(int year,int month,int data){
    		this.year=year;
    		this.month=month;
    		this.data=data;
    	}
    	
    	public int getYear() {
    		return year;
    	}
    	public void setYear(int year) {
    		this.year = year;
    	}
    	public int getMonth() {
    		return month;
    	}
    	public void setMonth(int month) {
    		this.month = month;
    	}
    	public int getData() {
    		return data;
    	}
    	public void setData(int data) {
    		this.data = data;
    	}
    	public String toString(){
    		return this.getYear()+"-"+this.getMonth()+"-"+this.getData();
    	}
    }
    
    
    public class Workers {//员工类
    	private int zno;//职工号
    	private String name;//姓名
    	private String sex;//性别
    	private Dep dept;//所在部门是部门类
    	private Data brith;
    	private Data time;
    	public Workers(int zno,String name,String sex){
    		this.name=name;
    		this.zno=zno;
    		this.sex=sex;
    	}
    	public int getZno() {
    		return zno;
    	}
    	public void setZno(int zno) {
    		this.zno = zno;
    	}
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public String getSex() {
    		return sex;
    	}
    	public void setSex(String sex) {
    		this.sex = sex;
    	}
    	public Dep getDept() {
    		return dept;
    	}
    	public void setDept(Dep dept) {
    		this.dept = dept;
    	}
    	public Data getTime() {
    		return time;
    	}
    	public void setTime(Data time) {
    		this.time = time;
    	}
    	public Data getBrith() {
    		return brith;
    	}
    	public void setBrith(Data brith) {
    		this.brith = brith;
    	}
    	public String toString(){
    		return "职工号:"+this.getZno()+"	姓名:"+this.getName()+"  	性别:"+this.getSex();
    	}
    }
    
    
    public class Dep {//部门类
    	private int bno;//部门名称
    	private String bname;//部门编号
    	private Workers manager;//一个部门有一个经理(员工)
    	private Workers emps1[];//一个部门多个员工
    	
    	public Dep(int bno,String bname){
    		this.bno=bno;
    		this.bname=bname;
    	}
    	public int getBno() {
    		return bno;
    	}
    	public void setBno(int bno) {
    		this.bno = bno;
    	}
    	public String getBname() {
    		return bname;
    	}
    	public void setBname(String bname) {
    		this.bname = bname;
    	}
    	
    	public Workers getManager() {
    		return manager;
    	}
    	public void setManager(Workers manager) {
    		this.manager = manager;
    	}
    	public Workers[] getEmps1() {
            return emps1;
    	}
    	public void setEmps1(Workers[] emps1) {
           this.emps1 = emps1;
      	}
    	public String toString(){
    		return "部门编号:"+this.getBno()+"	部门名称:"+this.getBname();
    	}
    }
    
    
    public class Text {//测试类
    	public static void main(String[] args) {
    	Dep d1=new Dep(01,"爱豆部");
    	Dep d2=new Dep(02,"后援部");
    	Workers w[]= {new Workers(1001,"刘一","男"),
    				new Workers(1002,"陈二","女"),//部门经理
    				new Workers(1003,"张三","男"),
    				new Workers(1004,"李四","男"),
    				new Workers(1005,"王五","男"),
    				new Workers(1006,"赵六","女"),
    				new Workers(1007,"孙七","女"),
    				new Workers(1008,"周八","男"),//部门经理
    				new Workers(1009,"吴九","男"),
    				new Workers(1010,"郑十","女")};
    	//设置部门和部门经理的关系
    	d1.setManager(w[1]);
    	d2.setManager(w[7]);
    	//设置员工和部门的关系
    	w[0].setDept(d1);
    	w[1].setDept(d1);
    	w[2].setDept(d1);
    	w[3].setDept(d1);
    	w[4].setDept(d1);
    	w[5].setDept(d2);
    	w[6].setDept(d2);
    	w[7].setDept(d2);
    	w[8].setDept(d2);
    	w[9].setDept(d2);
    	//设置员工生日和入职时间
    	Data a0=new Data(1991,05,16);
    	Data a1=new Data(1991,10,07);
    	Data a2=new Data(1992,04,20);
    	Data a3=new Data(1993,11,28);
    	Data a4=new Data(1989,06,07);
    	Data a5=new Data(1991,05,11);
    	Data a6=new Data(1990,10,01);
    	Data a7=new Data(1992,03,25);
    	Data a8=new Data(1993,12,30);
    	Data a9=new Data(1989,11,10);
    	
    	Data b0=new Data(2011,05,16);
    	Data b1=new Data(2013,10,07);
    	Data b2=new Data(2010,04,20);
    	Data b3=new Data(2011,11,28);
    	Data b4=new Data(2012,06,07);
    	Data b5=new Data(2012,07,11);
    	Data b6=new Data(2011,10,01);
    	Data b7=new Data(2010,04,25);
    	Data b8=new Data(2014,10,30);
    	Data b9=new Data(2013,10,10);
    	
    	w[0].setBrith(a0);
    	w[1].setBrith(a1);
    	w[2].setBrith(a2);
    	w[3].setBrith(a3);
    	w[4].setBrith(a4);
    	w[5].setBrith(a5);
    	w[6].setBrith(a6);
    	w[7].setBrith(a7);
    	w[8].setBrith(a8);
    	w[9].setBrith(a9);
    	
    	w[0].setTime(b0);
    	w[1].setTime(b1);
    	w[2].setTime(b2);
    	w[3].setTime(b3);
    	w[4].setTime(b4);
    	w[5].setTime(b5);
    	w[6].setTime(b6);
    	w[7].setTime(b7);
    	w[8].setTime(b8);
    	w[9].setTime(b9);
    	
    	d1.setEmps1(new Workers[] {w[0],w[1],w[2],w[3],w[4]});
    	d2.setEmps1(new Workers[] {w[5],w[6],w[7],w[8],w[9]});
    	System.out.println("根据员工得到信息、所在部门信息");
    	for(int i=0;i<w.length;i++) {
    		System.out.println(""+w[i].toString()+"	  生日:"+w[i].getBrith().toString());
    		
    		System.out.println("	入职时间: "+w[i].getTime().toString()+"	"+w[i].getDept().toString()+"	经理:"+w[i].getDept().getManager().getName());
    		System.out.println();
    		}
    	System.out.println();
    	System.out.println("根据部门得到部门信息、部门中员工的信息");
    	System.out.println(d1.toString()+"	部门经理  "+d1.getManager().getName());
    	System.out.println("部门1的员工");
    	for(int i=0;i<d1.getEmps1().length;i++) {
    		System.out.println(d1.getEmps1()[i].toString()+"	生日:"+d1.getEmps1()[i].getBrith().toString()+"	入职时间: "+d1.getEmps1()[i].getTime().toString());
    		}
    	System.out.println();
    	System.out.println(d2.toString()+"	部门经理  "+d2.getManager().getName());
    	System.out.println("部门2的员工");
    	for(int i=0;i<d2.getEmps1().length;i++) {
    		System.out.println(d2.getEmps1()[i].toString()+"	生日:"+d2.getEmps1()[i].getBrith().toString()+"	入职时间: "+d2.getEmps1()[i].getTime().toString());
    		}
    	}
    }
    

    1.用面向对象思想完成评分系统

    • 问题:第一次写类的,不太熟练,引用的时候出了很多错,不知道该怎么引用,谁又该引用谁
      解决方案:多问问别人,看看PPT

      4.统计文件

    • 问题:别的还可以,就是在分离文件扩展名,计数的时候出错
      原因:并没有考虑到万一一个名称输多个.,并没有想到比较之后 相同的用别的数替换
      解决方案:分离扩展名的时候,首先找到最后一个点,在把这个点之后的那些东西保存,计数的时候,先让自己和
      自己比,然后在让它和它之后的比较,比较相同的,可以用一个别的什么数替换掉 ,输出的时候,如果跟你替换的这个数不同
      就输出。

    5.类的设计

    • 问题:日期类的还好说,就是在别的类的 定义的时候 没搞清谁该定义成什么类的,测试类 声明关系 并分不清是把谁给谁
      还有调用 老是写错
      解决方案:在别的类定义 就比如生日 在员工类里定义日期类的生日,声明关系 就比如部门和部门经理 部门.set经理(经理的人选)
      调用嘛,自己想想,捋一捋关系,该怎么调。

    (三)代码托管(务必链接到你的项目)


    https://gitee.com/hebau_java_cs16/Java_CS02YJX/tree/master/哼

  • 相关阅读:
    1226 倒水问题
    1230 元素查找
    2152 滑雪
    1099 字串变换 2002年NOIP全国联赛提高组
    3027 线段覆盖 2
    P2066 机器分配
    spring的作用及优势---第一个spring示例
    密码框显示提示文字
    紫薇~还记得大明湖畔的HTML5智力拼图吗?
    细说javascript函数
  • 原文地址:https://www.cnblogs.com/zj1220/p/8681798.html
Copyright © 2011-2022 走看看