职业IT人-IT人生活圈

 找回密码
 成为会员
搜索
查看: 1690|回复: 0

张孝祥 -- 《Java就业培训教程》书中原码 6章

[复制链接]
weisheng 发表于 2006-11-14 07:22 | 显示全部楼层 |阅读模式
《Java就业培训教程》P218源码
程序清单:ReadLine.java
public class ReadLine
{
        public static void main(String [] args)
        {
                byte buf[]=new byte[1024];
                String strInfo=null;
                int pos=0;
                int ch=0;
                System.out.println(\"please enter info, input bye for exit:\");
                while(true)
                {
                        try
                        {            
                                ch=System.in.read();
                        }
                        catch(Exception e)
                        {
                                System.out.println(e.getMessage());
                        }
                        switch(ch)
                        {
                                case \'\\r\':
                                        break;
                                case \'\\n\':
                                        strInfo= new String(buf,0,pos);
                                        if(strInfo == \"bye\")
                                                return ;
                                        else
                                                System.out.println(strInfo);
                                                pos=0;
                                                break;
                                default:
                                        buf[pos++]=(byte)ch;
                        }
                }
        }
}

《Java就业培训教程》P221源码
程序清单:TestInteger.java
class TestInteger
{
        public static void main(String[] args)
        {
                int w = Integer.parseInt(args[0]);  //第一种方法
                int h = new  Integer(args[1]).intValue(); //第二种方法
                //int h = Integer.valueOf(args[1]).intValue(); //第三种方法
                for(int i=0;i<h;i++)
                {
                StringBuffer sb=new StringBuffer();
                for(int j=0 ;j<w;j++)
                {
                        sb.append(\'*\');
                }
                System.out.println(sb.toString());
                }
        }
}

《Java就业培训教程》P223源码
程序清单:TestVector.java
import java.util.*;  //下面用到的Vector类和Enumeration接口都在此包中
public class TestVector
{
        public static void main(String [] args)
        {
                int b=0;
                Vector v=new Vector();
                System.out.println(\"lease Enter Number:\");
                while(true)
                {
                        try
                        {
                                b= System.in.read();
                        }
                   catch(Exception e)
                        {
                                System.out.println(e.getMessage());
                        }
                        if(b==\'\\r\' || b== \'\\n\')
                                break;
                        else
                        {
                                int num=b-\'0\';
                                v.addElement(new Integer(num));
                        }
                }
                int sum=0;
                Enumeration e=v.elements();   
                while(e.hasMoreElements())
                {
                        Integer intObj=(Integer)e.nextElement();
                        sum += intObj.intValue();
                }
                System.out.println(sum);
        }
}

《Java就业培训教程》P225源码
程序清单:TestCollection.java
import java.util.*;  //ArrayList类和Iterator接口都在此包中
public class TestCollection
{
        public static void main(String [] args)
        {
        int b=0;
                ArrayList al=new ArrayList();
                System.out.println(\"lease Enter Number:\");
                while(true)
                {
                        try
                        {
                           b= System.in.read();
                        }
                        catch(Exception e)
                        {
                            System.out.println(e.getMessage());
                        }
                        if(b==\'\\r\' | b== \'\\n\')
                                break;
                        else
                        {
                                int num=b-\'0\';
                                al.add(new Integer(num));
                        }
                }
                int sum=0;
                Iterator itr=al.iterator();
                while(itr.hasNext())
                {
                        Integer intObj=(Integer)itr.next();
                        sum += intObj.intValue();
                }
                System.out.println(sum);
        }
}


《Java就业培训教程》P227源码
程序清单:TestSort.java
import java.util.*;
public class TestSort
{
        public static void main(String[] args)
        {
                ArrayList al=new ArrayList();
                al.add(new Integer(1));
                al.add(new Integer(3));
                al.add(new Integer(2));
                System.out.println(al.toString()); //排序前
                Collections.sort(al);
                System.out.println(al.toString()); //排序后
        }
}

《Java就业培训教程》P228源码
class MyKey
{
        private String name;
        private int age;
        public MyKey(String name,int age)
        {
                this.name = name;
                this.age = age;
        }
        public String toString()
        {
                return new String(name + \",\" + age);
        }
        public boolean equals(Object obj)
        {
                if(obj instanceof MyKey)
                {
                        MyKey objTemp = (MyKey)obj;
                        if(name.equals(objTemp.name) && age==objTemp.age)
                        {
                                return true;
                        }
                        else
                        {
                                return false;
                        }
                }
                //假如obj不是MyKey类的实例对象,它就不可能与当前对象相等了
                else
                {
                        return false;
                }
        }
        public int hashCode()
        {
                return name.hashCode() + age;
        }
}

《Java就业培训教程》P229源码
程序清单:HashtableTest.java
import java.util.*;
public class HashtableTest
{
        public static void main(String[] args)
        {
                Hashtable numbers=new Hashtable();
                numbers.put(new MyKey(\"zhangsan\",18),new Integer(1));
                numbers.put(new MyKey(\"lisi\",15),new Integer(2));
                numbers.put(new MyKey(\"wangwu\",20),new Integer(3));
                Enumeration e=numbers.keys();
                while(e.hasMoreElements())
                {
                        MyKey key=(MyKey)e.nextElement();
                        System.out.print(key.toString()+\"=\");
                        System.out.println(numbers.get(key).toString());
                }
        }
}
《Java就业培训教程》P231源码
程序清单:PropertiesFile.java
import java.util.*;
import java.io.*;
class PropertiesFile
{
        public static void main(String[] args)
        {
                Properties settings=new Properties();
                try
                {
                        settings.load(new FileInputStream(\"c:\\\\count.txt\"));
                }
                catch(Exception e)
                {
                        settings.setProperty(\"Count\",new Integer(0).toString());
                }
                int c=Integer.parseInt(settings.getProperty(\"Count\"))+1;
                System.out.println(\"这是本程序第\"+c+\"次被使用\");
                settings.put(\"Count\",new Integer(c).toString());
                try
                {
                        settings.store(new FileOutputStream(\"c:\\\\count.txt\"),
                        \"This Program is used:\");
                }
                catch(Exception e)
                {
                        System.out.println(e.getMessage());
                }
        }
}
《Java就业培训教程》P233源码
程序清单:TestProperties
import java.util.*;
public class SystemInfo
{
        public static void main(String[] args)
        {
                Properties sp=System.getProperties();
                Enumeration e=sp.propertyNames();
                while(e.hasMoreElements())
                {
                        String key=(String)e.nextElement();
                        System.out.println(key+\" = \"+sp.getProperty(key));
                }
        }
}
《Java就业培训教程》P234源码
程序清单:TestRuntime.java
public class TestRuntime
{
        public static void main(String[] args)
        {
                Process p=null;
        try
                {
                        p=Runtime.getRuntime().exec(\"notepad.exe TestRuntime.java\");
                          Thread.sleep(5000);
                   }
            catch(Exception e)
            {
                     System.out.println(e.getMessage());
            }
            p.destroy();
        }
}
《Java就业培训教程》P236源码
程序清单:TestDateFormat.java
import java.util.*;
import java.text.*;
public class TestDateFormat
{
        public static void main(String[] args)
        {
                SimpleDateFormat sdf1=new SimpleDateFormat(\"yyyy-MM-dd\");
                SimpleDateFormat sdf2=new SimpleDateFormat(\"yyyy年MM月dd日\");
                try
                {
                        Date d=sdf1.parse(\"2003-03-15\");
                        System.out.println(sdf2.format(d));
                }
                catch(Exception e)
                {
                        System.out.println(e.getMessage());
                }
        }
}
您需要登录后才可以回帖 登录 | 成为会员

本版积分规则

QQ|手机版|小黑屋|网站帮助|职业IT人-IT人生活圈 ( 粤ICP备12053935号-1 )|网站地图
本站文章版权归原发布者及原出处所有。内容为作者个人观点,并不代表本站赞同其观点和对其真实性负责,本站只提供参考并不构成任何投资及应用建议。本站是信息平台,网站上部分文章为转载,并不用于任何商业目的,我们已经尽可能的对作者和来源进行了通告,但是能力有限或疏忽造成漏登,请及时联系我们,我们将根据著作权人的要求立即更正或者删除有关内容。

GMT+8, 2024-5-12 03:06 , Processed in 0.170110 second(s), 20 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表