zoukankan      html  css  js  c++  java
  • JAVA 5.17习题

    1.编写并测试一个代表地址的Address类,地址信息由国家、省份、城市、街道、邮编组成,并可以返回完整的地址信息。

    //=================================================
    // File Name       :	Address_demo
    //------------------------------------------------------------------------------
    // Author          :	Common
    
    
    // 内部类名:Address
    // 属性:
    // 方法:
    class Address{
    	private String country;			//定义国家 
    	private String province;		//定义省份
    	private String city;				//定义城市
    	private String street;			//定义街道
    	private String  zipcode;		//定义邮编
    	
    	public Address(String country,String province,String city,String street,String  zipcode){		//构造方法设置地址
    		this.country = country;
    		this.province = province;
    		this.city = city;
    		this.street = street;
    		this.zipcode = zipcode;
    	}
    	
    	public String getCountry(){
    		return this.country;
    	}
    
    	public void setCountry(String country) {
    		this.country = country;
    	}
    	
    	public String getProvince() {
    		return this.province;
    	}
    
    	public void setProvince(String province) {
    		this.province = province;
    	}
    
    	public String getCity() {
    		return this.city;
    	}
    
    	public void setCity(String city) {
    		this.city = city;
    	}
    
    	public String getStreet() {
    		return this.street;
    	}
    
    	public void setStreet(String street) {
    		this.street = street;
    	}
    
    	public String getZipcode() {
    		return this.zipcode;
    	}
    
    	public void setZipcode(String zipcode) {
    		this.zipcode = zipcode;
    	}
    
    	public void print(){		//1.定义全部输出方法
    		System.out.println("国家"+this.country+"
    "+"省份"+this.province+"
    "+"城市"+this.city+"
    "+"街道"+this.street+"
    "+"邮编"+this.zipcode);
    	}
    }
    
    
    
    
    //主类
    //Function        : 	Address
    public class Address_demo {
    
    	public static void main(String[] args) {
    		// TODO 自动生成的方法存根
    		Address add = new Address("中国","福建","涵江","中山路","351111");
    		add.print();
    	}
    
    }
    

    2.定义并测试一个代表员工的employee类。员工属性包括“编号”、“姓名”、“基本薪水”、“薪水增长额”,还包括计算薪水增长额及计算增长后的工资总额的操作方法

    //=================================================
    // File Name       :	Employee_demo
    //------------------------------------------------------------------------------
    // Author          :	Common
    
    
    // 类名:Employee
    // 属性:
    // 方法:
    class Employee{
    	private String id;
    	private String name;
    	private int basic_Salary;
    	private int growth_Salary;
    	
    	public Employee(String id, String name, int basic_Salary, int growth_Salary) {
    		this.id = id;
    		this.name = name;
    		this.basic_Salary = basic_Salary;
    		this.growth_Salary = growth_Salary;
    	}
    	
    	public String getId() {
    		return id;
    	}
    	public void setId(String id) {
    		this.id = id;
    	}
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public int getBasic_Salary() {
    		return basic_Salary;
    	}
    	public void setBasic_Salary(int basic_Salary) {
    		this.basic_Salary = basic_Salary;
    	}
    	public int getGrowth_Salary() {
    		return growth_Salary;
    	}
    	public void setGrowth_Salary(int growth_Salary) {
    		this.growth_Salary = growth_Salary;
    	}
    	
    	public void print(){
    		System.out.println("员工编号"+this.id+"
    "+"员工姓名"+this.name+"
    "+"基本工资"+this.basic_Salary+"
    "+"奖金"+this.growth_Salary+"
    "+"总额"+(this.basic_Salary+this.growth_Salary));
    	}
    	
    }
    
    
    //主类
    //Function        : 	Employee
    public class Employee_demo {
    
    	public static void main(String[] args) {
    		// TODO 自动生成的方法存根
    		Employee emp = new Employee("1", "Jack", 10000, 5000);
    		emp.print();
    	}
    
    }
    

    编写程序,统计处字符串“want you to know one thing”中字母n和字母o的出现次数

    //=================================================
    // File Name       :	string_Count_demo
    //------------------------------------------------------------------------------
    // Author          :	Common
    
    
    // 类名:string_Count
    // 属性:
    // 方法:
    class string_Count{
    	private int num;
    	private char str[];
    	private char c;
    	
    	public string_Count(String str,char c){
    		this.str = str.toCharArray();
    		this.c = c;
    	}
    	
    	public void print(){
    		for(int i=0;i<this.str.length;i++){
    			if(str[i] == this.c){
    				this.num++;
    			}
    		}
    		System.out.println("有"+this.c+"的个数为:"+this.num+"
    ");
    		this.num = 0;		//对num清零
    	}
    	
    	
    }
    
    //主类
    //Function        : 	string_Count
    public class string_Count_demo {
    
    	public static void main(String[] args) {
    		// TODO 自动生成的方法存根
    		string_Count demo = new string_Count("want you to know one thing",'n');
    		demo.print();
    		string_Count demo_ = new string_Count("want you to know one thing",'o');
    		demo_.print();
    	}
    
    }
    

    设计一个表示用户的user类,类中的变量有用户名/口令和记录用户个数的变量,定义类的3个构造方法(无参、为用户名赋值、为用户名和口令赋值)、获取和设置口令的方法和返回类信息的方法

    //=================================================
    // File Name       :	User_demo
    //------------------------------------------------------------------------------
    // Author          :	Common
    
    
    // 类名:user
    // 属性:
    // 方法:
    class user{
    	private String name;
    	private String pwd;
    	private static int user_num;	//声明成static的时候产生多个对象的时候,修改一个对象就行修改所有的对象
    	
    	public user() {	//构造方法无参
    		user_num++;	//用静态方式访问
    	}
    	
    	public user(String name) {	//构造方法,为用户名赋值
    		this.name = name;
    		user_num++;
    	}
    
    	public user(String name, String pwd) {	//构造方法,为用户名和口令赋值
    		this.name = name;
    		this.pwd = pwd;
    		user_num++;
    	}
    
    	public String getPwd() {
    		return this.pwd;
    	}
    	public void setPwd(String pwd) {
    		this.pwd = pwd;
    	}
    	
    	public void print(){
    		System.out.println("用户名:"+this.name+"
    "+"密码:"+this.pwd+"
    "+"用户个数"+user_num);
    	}
    	
    }
    
    
    //主类
    //Function        : 	user
    public class User_demo {
    
    	public static void main(String[] args) {
    		// TODO 自动生成的方法存根
    		user zhangsan = new user();
    		zhangsan.print();
    		user lisi = new user("李四");
    		lisi.print();
    		user wangwu = new user("王五","12345");
    		wangwu.print();
    		wangwu.setPwd("12345678");
    		System.out.println("password:"+wangwu.getPwd());
    		wangwu.print();
    	}
    
    }
    

    字符串操作

    //=================================================
    // File Name       :	String_operation_demo
    //------------------------------------------------------------------------------
    // Author          :	Common
    
    
    public class String_operation_demo {
    
    	public static void main(String[] args) {
    		// TODO 自动生成的方法存根
    		String str = "Java技术学习班  20070326";
    		System.out.println(str.substring(11));
    		
    		String str1 = "MLDN JAVA";
    		String str2 = str1.replace("JAVA", "J2EE");
    		System.out.println(str2);
    		
    		System.out.println(str.charAt(8));
    		
    		System.out.println(str.replace("0", ""));
    		
    		System.out.println(str.replace(" ", ""));
    		
    		String str3 = "350101199302147777";
    		System.out.println("birthday:"+str3.substring(6, 14));
    	}
    
    }
    

    编写一个公司员工类

    //=================================================
    // File Name       :	Cooperation_employee_demo
    //------------------------------------------------------------------------------
    // Author          :	Common
    
    
    // 类名:Cooperation_employee
    // 属性:
    // 方法:
    class Cooperation_employee{
    	private String id;
    	private String name;
    	private int salary;
    	private String department;
    	
    	public Cooperation_employee(){	//无参
    
    	}
    	
    	public Cooperation_employee(String id){	//单参
    		this.id = id;
    		this.name = "无名氏";
    		this.salary = 0;
    		this.department = "未定";
    	}
    	
    	public Cooperation_employee(String id,String name){	//双参
    		this.id = id;
    		this.name = name;
    		this.salary = 1000;
    		this.department = "后勤";
    	}
    	
    	public Cooperation_employee(String id,String name,int salary,String department){	//4参
    		this.id = id;
    		this.name = name;
    		this.salary = salary;
    		this.department = department;
    	}
    	
    	public void print(){
    		System.out.println("员工号"+this.id+"
    "+"姓名"+this.name+"
    "+"薪水"+this.salary+"
    "+"部门"+this.department);
    	}
    }
    
    
    public class Cooperation_employee_demo {
    
    	public static void main(String[] args) {
    		// TODO 自动生成的方法存根
    		Cooperation_employee no_1  = new Cooperation_employee();
    		no_1.print();
    		Cooperation_employee no_2  = new Cooperation_employee("001");
    		no_2.print();
    		Cooperation_employee no_3  = new Cooperation_employee("001","张三");
    		no_3.print();
    		Cooperation_employee no_4  = new Cooperation_employee("002","李四",1000,"开发");
    		no_4.print();
    	}
    
    }
    

    声明一个图书类、其数据成员位书名、编号(利用静态变量实现自动编号)、书价,并拥有静态数据成员册数、记录图书的总册数,在构造方法中利用此静态变量为对象的编号赋值,在主方法中定义对象数组,并求出总册数

    //=================================================
    // File Name       :	book_demo
    //------------------------------------------------------------------------------
    // Author          :	Common
    
    
    // 类名:book
    // 属性:
    // 方法:
    class book{
    	private String name;
    	private String id;
    	private double price;
    	private static int count;
    	
    	public book(String name,double price){
    		count++;
    		this.name = name;
    		this.price = price;
    		this.id = "000"+count;
    	}
    	
    	public void print(){
    		System.out.println("书名:"+this.name+"
    "+"ID:"+this.id+"
    "+"价格:"+this.price+"
    "+"书的总数"+count);
    	}
    	
    }
    
    public class book_demo {
    
    	public static void main(String[] args) {
    		// TODO 自动生成的方法存根
    		book[] book_array = new book[10];
    		for(int i=0;i<book_array.length;i++){
    			book_array[i] = new book("JAVA编程思想",19.8);
    		}
    		book_array[1].print();
    	}
    
    }
    
  • 相关阅读:
    最详尽的IntelliJ IDEA项目web项目搭建!!!!!!
    Unable to locate JAR/zip in file system as specified by the driver definition: ojdbc14.jar
    Caused by: org.hibernate.HibernateException: Unable to build the default ValidatorFactory
    MySQL闪退
    mysql:unknown variable 'default -collation=utf8_general_ci'
    更改文本的编码jsp.xml.java
    save is not valid without active transaction
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'deptDao_a' defined in class path resource [beansAndHibernate.xml]: Cannot resolve reference to bean 'sessionFact
    java.lang.ClassNotFoundException: org.springframework.orm.hibernate3.LocalSessionFactoryBean
    linux常用Java程序员使用命令(二)
  • 原文地址:https://www.cnblogs.com/tonglin0325/p/5235290.html
Copyright © 2011-2022 走看看