java面向对象编程实例讲解_Java面向对象编程实例详解

Java是一种面向对象的语言,是实现面向对象编程的强大工具。但如何在编程中实际运用并发挥其最大效能呢?本文通过一个实际Java程序的开发过程,详细说明了如何使用面向对象实现Java编程。

我们要实现的Java应用程序是:当用户输入一个球体的半径,程序将显示该球体的体积与表面积。在您阅读下文以前,请您自己思考一分钟,您将如何设计该Java应用程序。

一、普通实现方法

我相信大多数程序员要实现上述功能的程序,非常迅速地、自信地将采用下面的实现代码:

class Sphere

{

public static void main(String[] args)

{

EasyReader console = new EasyReader();

System.out.print("Enter the radius: ");

double radius = console.readDouble();

System.out.println("Radius = " + radius);

double volume = 4.0 / 3.0 * Math.PI * radius * radius * radius;

System.out.println("Volume = " + volume);

double surfArea = 4.0 * Math.PI * radius * radius;

System.out.println("Surface area = " + surfArea);

}

}

EasyReader类代码如下:

import java.io.*;

public class EasyReader

{

protected String myFileName;

protected BufferedReader myInFile;

protected int myErrorFlags = 0;

protected static final int OPENERROR = 0x0001;

protected static final int CLOSEERROR = 0x0002;

protected static final int READERROR = 0x0004;

protected static final int EOF = 0x0100;

/**

* Constructor. Prepares console (System.in) for reading

*/

public EasyReader()

{

myFileName = null;

myErrorFlags = 0;

myInFile = new BufferedReader(

new InputStreamReader(System.in), 128);

}

/**

* Constructor. opens a file for reading

* @param fileName the name or pathname of the file

*/

public EasyReader(String fileName)

{

myFileName = fileName;

myErrorFlags = 0;

try

{

myInFile = new BufferedReader(new FileReader(fileName), 1024);

}

catch (FileNotFoundException e)

{

myErrorFlags |= OPENERROR;

myFileName = null;

}

}

/**

* Closes the file

*/

public void close()

{

if (myFileName == null)

return;

try

{

myInFile.close();

}

catch (IOException e)

{

System.err.println("Error closing " + myFileName + "

");

myErrorFlags |= CLOSEERROR;

}

}

/**

* Checks the status of the file

* @return true if en error occurred opening or reading the file,

* false otherwise

*/

public boolean bad()

{

return myErrorFlags != 0;

}

/**

* Checks the EOF status of the file

* @return true if EOF was encountered in the previous read

* operation, false otherwise

*/

public boolean eof()

{

return (myErrorFlags & EOF) != 0;

}

private boolean ready() throws IOException

{

return myFileName == null || myInFile.ready();

}

/**

* Reads the next character from a file (any character including

* a space or a newline character).

* @return character read or null character

* (Unicode 0) if trying to read beyond the EOF

*/

public char readChar()

{

char ch = 'u0000';

try

{

if (ready())

{

ch = (char)myInFile.read();

}

}

catch (IOException e)

{

if (myFileName != null)

System.err.println("Error reading " + myFileName + "

");

myErrorFlags |= READERROR;

}

if (ch == 'u0000')

myErrorFlags |= EOF;

return ch;

}

/**

* Reads from the current position in the file up to and including

* the next newline character. The newline character is thrown away

* @return the read string (excluding the newline character) or

* null if trying to read beyond the EOF

*/

public String readLine()

{

String s = null;

try

{

s = myInFile.readLine();

}

catch (IOException e)

{

if (myFileName != null)

System.err.println("Error reading " + myFileName + "

");

myErrorFlags |= READERROR;

}

if (s == null)

myErrorFlags |= EOF;

return s;

}

/**

* Skips whitespace and reads the next word (a string of consecutive

* non-whitespace characters (up to but excluding the next space,

* newline, etc.)

* @return the read string or null if trying to read beyond the EOF

*/

public String readWord()

{

StringBuffer buffer = new StringBuffer(128);

char ch = ' ';

int count = 0;

String s = null;

try

{

while (ready() && Character.isWhitespace(ch))

ch = (char)myInFile.read();

while (ready() && !Character.isWhitespace(ch))

{

count++;

buffer.append(ch);

myInFile.mark(1);

ch = (char)myInFile.read();

};

if (count > 0)

{

myInFile.reset();

s = buffer.toString();

}

else

{

myErrorFlags |= EOF;

}

}

catch (IOException e)

{

if (myFileName != null)

System.err.println("Error reading " + myFileName + "

");

myErrorFlags |= READERROR;

}

return s;

}

/**

* Reads the next integer (without validating its format)

* @return the integer read or 0 if trying to read beyond the EOF

*/

public int readInt()

{

String s = readWord();

if (s != null)

return Integer.parseInt(s);

else

return 0;

}

/**

* Reads the next double (without validating its format)

* @return the number read or 0 if trying to read beyond the EOF

*/

public double readDouble()

{

String s = readWord();

if (s != null)

return Double.parseDouble(s);

// in Java 1, use: return Double.valueOf(s).doubleValue();

else

return 0.0;

}

}

以下是程序的输出结果画面。

image002.jpg

在上述程序中,实现了球体表面积和体积的算法,并通过EasyReader类轻松实现了控制台的输入,但是该程序只是完成了程序所要求的功能,并计算出了结果。该程序不是一个好的设计程序,它既没采用结构化编程,也没有发挥Java语言的强大效能,更不是面向对象编程。

实际上,从程序设计的角度来看,以上程序是一个非常糟糕的设计程序。首先,用户界面与计算公式混合在一起。请记住:对于任何编程语言,用户界面必须与计算或处理过程分离开来。另外,输出的结果太难看,数值包括太多的小数。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://xiahunao.cn/news/1383232.html

如若内容造成侵权/违法违规/事实不符,请联系瞎胡闹网进行投诉反馈,一经查实,立即删除!

相关文章

高端百度地图开发2:自定义水滴头像(鼠标事件、API封装对接)

高端百度地图开发系列 高端百度地图开发1:自定义水滴头像(自定义标注覆盖物、Overlay覆盖类) 自定义水滴头像之鼠标事件、API封装对接 高端百度地图开发系列一、添加自定义覆盖物的方法二、对接API数据1.获取API数据(模拟)2.遍历数据3.添加自…

2023年中国智慧公安行业发展现况及发展趋势分析:数据化建设的覆盖范围不断扩大[图]

智慧公安基于互联网、物联网、云计算、智能引擎、视频技术、数据挖掘、知识管理为技术支撑,公安信息化为核心,通过互联互通、物联化、智能方式促进公安系统各功能模块的高度集成、协同作战实现警务信息化“强度整合、高度共享、深度应用”警察发展的新概…

写一个简单炫酷的app程序的打开动画

app也是有颜值,动画就是一种化妆术,他让你的app更加炫酷,首先给你看下图 首先呢,先普及下总体的框架知识,有三种动画 (1)Drawable Animation:逐帧动画,就像电影一样,一帧一帧&am…

安卓开机动画的制作

开机动画的存储位置: system/media/bootanimation.zip。 bootanimation.zip的文件结构:包含一个desc.txt和N个文件夹(一般两个)。desc.txt是用来指导如何执行动画页面。文件夹里存放要播放的照片(支持png和jpg格式&am…

安卓基础动画

在开发的过程中,动画可以实现很好的效果,比如对于图片的旋转,电子罗盘的都可以利用动画来时实现。但是对于动画不太熟悉,经常想用却不会用,特地写这篇博客,记录一下对于动画的学习。 主要学习帧动画和Objec…

安卓动画:

Android的三种动画,即: •View Animation(视图动画) •Drawable Animation(帧动画) •Property Animation(属性动画) Drawable Animation(帧动画)&#xff1…

安卓手机开机动画制作原理教程

先说开机动画实现原理: Android的动画是由一系列的连续PNG图片作为帧组成的动画形式。 不是合成为一张GIF图片,而是一个文件包,将各帧PNG图片以压缩方式保存。 这个保存的文件名就是bootanimation.zip,这个文件是zip压缩文件&a…

安卓之动画制作

1.知识图谱 (补间动画和帧动画) 1.补间动画的效果图: (alpha透明度) (ratate旋转) XML代码&#xf…

Android BT

整个bluedroid可以分为两大模块:BTIF,BTE BTIF:提供bluedroid对外的接口;作为bluedroid与上层的framework提供interface。所以它处于bluedroid的顶层。 BTE:bluedroid的内部处理,又细分为BTA,BTU&#xf…

04.BT下载

04.BT下载 下载,是很多网络用户必备的需求之一。以往我们使用现成工具迅雷,我们必须保持开机,会员,合法等等要求,但是依托服务器,我们其实也可以搭建BT下载工具,只要你的硬盘够用,把…

BackTrack5(BT5)各版本下载

BT5R3(最新版本)http://www.nigesb.com/backtrack-5-r3-released.html BT5R2 KDE版32位: http://ftp.halifax.rwth-aachen.de/backtrack/BT5R2-KDE-32.iso GNOME32位:http://ftp.halifax.rwth-aachen.de/backtrack/BT5R2-GNOME-32.iso BT5R1 KDE版32位…

mysql 存储用户头像_node+vue用户头像处理上传并保存

2017年7月14日19:21:29 ,最近做个网站,需要有用户头像裁剪上传功能,具体流程是用户在本地选择图片,前端将图片裁剪好发给后端,后端接收到图片将它保存起来,并将信息存入数据库。 先说一下环境后端是express…

java 根据用户名生成头像。

先上效果图 自行根据需求修改代码 package com.space; import java.awt.*; import java.awt.geom.RoundRectangle2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.Random; import java.util.regex.Matcher; im…

支付宝小程序serverless---获取用户信息(头像)并保存到云数据库

支付宝小程序serverless—获取用户信息(头像)并保存到云数据库 博客说明 文章所涉及的资料来自互联网整理和个人总结,意在于个人学习和经验汇总,如有什么地方侵权,请联系本人删除,谢谢! 我又回…

湘大 XTU OJ 1260 Completed String 题解(非常详细):建立数组下标和数组元素之间的映射关系 ~scanf

一、链接 1260 Completed String 二、题目 题目描述 给一个字符串,请判断字符串是否出现了所有的英文字母(不区分大小写)。 输入 每行一个只含英文字母的字符串,长度不超过1000。 输出 每行输出一个样例的结果&#xff0c…

openCV配置+cmake操作记录--Ubuntu 16.04版本

项目需要openCV相关操作,于是找了好多教程,琢磨了好久,写个笔记。 更新ubuntu上的一些库: sudo apt-get update sudo apt-get upgrade 安装一些库:(虽然我现在也不明白这玩意,我是纯小白/(ㄒoㄒ)/~~) sudo apt-get install cmake sudo apt-get install build-essen…

笔记2:openCV打开摄像头显示到QT的QLabel--Ubuntu系统

目的: 通过openCV调用摄像头,把捕捉到的画面显示到QT的QLabel控件上。 听起来很easy的样子,我的coding能力很弱,可以找个我是理学院出身的借口,但现实是谁会听你的借口呢,希望这笔记不是给我未来参考的,而是为了博未来的自己一笑,那样才能说明我是进步的哈。 效果:…

【二叉树】1-5,理论基础、前中后序遍历的递归法和迭代法、层序遍历

理论基础、前中后序遍历的递归法和迭代法、层序遍历 1,二叉树的种类满二叉树完全二叉树二叉搜索树平衡二叉搜索树 2,存储方式链式存储线式存储 3,二叉树的遍历深度优先搜索前序遍历(递归法、迭代法)中序遍历&#xff0…

fork创建多个子进程

fork创建多个子进程 示例代码 fork1.c #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <unistd.h>int main(int argc,char **argv) {int i, j;pid_t pid;for (i 0; i < 3; i){pid fork();if (pid < 0){perror(&q…

苹果6换屏多钱_iPhone12屏幕维修多少钱 苹果12换屏价格汇总

苹果iPhone12系列手机如果屏幕坏了要维修换屏的话&#xff0c;需要多少钱呢&#xff0c;官方的换屏价格是多少&#xff0c;这里我们来了解下iPhone12系列手机官方渠道换屏价格。 1、iPhone12保外屏幕维修费用 2149元&#xff0c;iPhone12Pro屏幕维修费用2149元&#xff0c;由于…