免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1066 | 回复: 0
打印 上一主题 下一主题

第一次JAVA作业 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-09-29 04:56 |只看该作者 |倒序浏览
前言:很扯淡啊,竟然晕晕糊糊的写完了第一个java程序,分成了两个版本的,面向对象和面向过程.具体内容请详细往下看.(GPL)
实验报告 一
实验目的:
1、  安装JDK;
2、  设置环境变量;
3、  编写程序,完成下面两个功能:
(1)计算n的阶乘(n!),n!=1×2×3×…n
(2)计算数学常数e的值:e=1+1/1!+1/2!+1/3!+...
实验过程:
关于前两个要求,安装jdk和设置环境变量,可以参考我的blog,其中又M$和Linux下的配置方案,这里不再浪费纸张. (我的blog http://netyang.cublog.cn)
声明:本次实验的题目是两个小型的题目.其中一个是另外一个的一个子集.因为java学的不是太熟,所以我做了两个版本的.其中一是面向过程的设计思想,二是面向对象的思想,因为太简单了也没有仔细考虑如何使用模式让程序的通用性更大,更是将一部分的类直接声明成了final,完全不考虑继承的关系.
第一部分:面向过程的思想.
这里我直接用源代码来表示我的思路.注:两个函数定义在一起了,可以通过函数名加以区别.
package netyang;
import java.io.*;
public class Factorial {
    static BufferedReader createreader(){
       BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
       return stdin;
    }
    static int getselect() throws IOException {
       System.out.println("----------------------------");
       System.out.println("     Factorial         ");
       System.out.println(" === Power By Netyang ===");
       System.out.println(" =====   Menu   =====   ");
       System.out.println(" 1.Factorial              ");
       System.out.println(" 2.Value of E         ");
       System.out.println(" 3.Exit               ");
       System.out.println("----------------------------");
       BufferedReader stdin=createreader();
       String gets=stdin.readLine();
       int selected=Integer.parseInt(gets);
       return selected;
    }
    static void display() throws IOException {
       int selected=getselect();
       while (selected!=3) {
            if(selected==1)
           {
               myFactorial();
               selected=2007;
            }
            else if(selected==2)
            {
               myvalueE();
               selected=2007;
            }
            else
            {
               selected=getselect();
            }
        }
       return ;
    }
    public static void main(String[] args) throws IOException {
       display();
       return;
    }
    static void myvalueE() throws IOException {
       System.out.println("Please Input a Number:");
       BufferedReader stdin=createreader();
       String gets=stdin.readLine();
       int times=Integer.parseInt(gets);;
       double e=1.0;
       for(int i=1;i
           e+=1/valueE(i);
       }
       System.out.println("The Value of E is:"+e);
    }
    static double valueE(int times) {
       double simple=1.0;
       return getfactorial(times,simple);
    }
   
    static void myFactorial ()throws IOException{
       System.out.println("Please Input a Number:");
       BufferedReader stdin=createreader();
       String gets=stdin.readLine();
       int m=Integer.parseInt(gets);
       double s=0;
       double sum=1;
       System.out.println("Here are Status:");
       s=getfactorial(m,sum);
       System.out.println("The Factorial of "+m+" is:"+s);
       return ;
    }
    static double getfactorial(int s,double m){
       if(s==1){
           return m;
       }
       else{
           m=m*s;
           s--;
           return getfactorial(s,m);
       }
    }
}



第二部分:面向对象的思想
package netyang;
import java.io.*;
public class Bclass {
       public static void main(String[] args) {
              menu mymenu=new menu();
              mymenu.display();
       }
}
final class menu
{
       int _selected;
       private int get_selected() {
              return _selected;
       }
       private void set_selected(int _selected) {
              this._selected = _selected;
       }
       BufferedReader createreader(){
              BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
              return stdin;
       }
       int getselect() throws IOException{
              BufferedReader stdin=createreader();
              String gets=stdin.readLine();
              int selected=Integer.parseInt(gets);
              return selected;
       }
       void display(){
              System.out.println("----------------------------");
              System.out.println("             Factorial                ");
              System.out.println("      === Power By Netyang ===");
              System.out.println("     =====   Menu   =====    ");
              System.out.println("     1.Factorial                           ");
              System.out.println("     2.Value of E                 ");
              System.out.println("     3.Exit                                 ");
              System.out.println("----------------------------");
              System.out.println("Please Choice :");
              act();
       }
       void act(){
              try{
                     int s=this.getselect();
                     this.set_selected(s);
              }
              catch(IOException e){
                     System.out.println(e.toString());
              }
              judge();
       }
       void judge(){
              while(this.get_selected()!=3){
                     if(this.get_selected()==1){
                            factorialinit();
                            this.set_selected(2007);
                     }
                     else if(this.get_selected()==2){
                            valueEinit();
                            this.set_selected(2007);
                     }
                     else
                            this.display();
              }
              return ;
       }
       void factorialinit(){
              int s=0;
              System.out.println("Please Input a number you want:");
              try{
                     s=this.getselect();
              }
              catch(IOException e){
                     System.out.println(e.toString());
              }
              factorial myf=new factorial();
              myf.set_value(s);
              myf.keep();
              myf.count();
              myf.display();
       }
       void valueEinit(){
              int s=0;
              System.out.println("Please Input a number you want:");
              try{
                     s=this.getselect();
              }
catch(IOException e){
                     System.out.println(e.toString());
              }
              valueE myf=new valueE();
              myf.set_value(s);
              myf.keep();
              myf.count();
              myf.display();
       }
}
final class factorial{
       int _value;
       double _sum=1.0;
       int _kvalue;
       private int get_value() {
              return _value;
       }
       public void set_value(int _value) {
              this._value = _value;
       }
       private double get_sum() {
              return _sum;
       }
       private void set_sum(double _sum) {
              this._sum = _sum;
       }
       void keep(){
              this.set_kvalue(this.get_value());
       }
       double count(){
              if(this.get_value()==1){
              //     System.out.println("When s=1,sum="+this.get_sum());
              //     this.display();
                     return this.get_sum();
              }
              else{
                     double m=this.get_sum()*this.get_value();
                     this.set_sum(m);            
              //     System.out.println("When s="+this.get_value()+",sum="+this.get_sum());
                     int s=this.get_value();
                     s--;
                     this.set_value(s);
                     return this.count();
              }
       }
       void display(){
              System.out.println("The foctorial value of "+this.get_kvalue()+" Is: "+this.get_sum());
       }
       private int get_kvalue() {
              return _kvalue;
       }
       private void set_kvalue(int _kvalue) {
              this._kvalue = _kvalue;
       }     
}
final class valueE{
       int _kvalue;
       double _sum=1.0;
       int _value;
       private int get_kvalue() {
              return _kvalue;
       }
       private void set_kvalue(int _kvalue) {
              this._kvalue = _kvalue;
       }
       private double get_sum() {
              return _sum;
       }
       private void set_sum(double _sum) {
              this._sum = _sum;
       }
       private int get_value() {
              return _value;
       }
       public void set_value(int _value) {
              this._value = _value;
       }
       void keep(){
              this.set_kvalue(this.get_value());
       }
       void count(){
              for(int i=1;i
                     factorial myf=new factorial();
                     myf.set_value(i);
                     myf.keep();
                     double m=1/(myf.count());
                     m+=this.get_sum();
                     this.set_sum(m);
                     m=0.0;
              }
//            display();
       }
       void display(){
              System.out.println("The Value of E in "+this.get_kvalue()+"Times is "+this.get_sum());
       }
}
为了节约纸张,我截取了Eclips的部分运行结果.更详细的内容请参考附件.将该页面的代码用记事本转存为BClass.java即可使用.
实验总结:
       这个程序太简单了,但是也是耗费了我四个小时左右,为什么呢?一是对Eclips不是太熟,而是Java语法忘的七七八八的,基本上是从网上学的,还好能应付这些小的题目.如果一定要总结,我想针对面向对象思想的代码进行总结,从代码上分析做的好像还是有点乱,应该好好重写menu类,因为里面出现了太多的if else嵌套.menu类中的act函数有点类似于策略者模式提供算法选择支持.还有应该值得一提的就是这个属性权限的应用,可以看到我的类中全部的get属性都是private的,但是有一个set属性是public属性,为什么呢?因为要和外界联系,所以必须公开一个变量和外界交互数据.同时也保证类的封装性.提起封装性《老子》八十章《邻国相望,鸡犬之声相闻》中的“邻国相望,鸡犬之声相闻,民至老死不相往来”这一句点出了封装的最高境界.除非王给了通行令,否则民至老死不项往来.我的代码还没有到这种地步,不过还算鲜明了.因为学的不是太深入.这是比较菜的代码,本来想搞个漂亮的UI,因为项目上还有很多事情要做,所以只好草草的结束.


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/8036/showart_178226.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP