publicabstractclass Shape {
目前創(chuàng)新互聯(lián)已為上1000+的企業(yè)提供了網(wǎng)站建設(shè)、域名、雅安服務(wù)器托管、網(wǎng)站托管運(yùn)營(yíng)、企業(yè)網(wǎng)站設(shè)計(jì)、元謀網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。
publicabstractvoid area();
}
class Circle extends Shape {
privatedoubleradius;
privatedoubleS;
Circle(double radius) {
this.radius = radius;
}
publicvoid area() {
S = 3.14 * radius * radius;
System.out.println(S);
}
}
class Rect extends Shape {
privatedoublelength;
privatedoublewidth;
privatedoubleS;
Rect(double length,double width) {
this.length = length;
this.width = width;
}
publicvoid area() {
S = length * width;
System.out.println(S);
}
}
class Test {
publicstaticvoid main(String[] args) {
Circle a = new Circle(3);
a.area();
Rect b = new Rect(3,4);
b.area();
}
}
用Java代碼中的for循環(huán)可以打印出各種三角形,便于熟悉for循環(huán)的特性,即外環(huán)循環(huán)執(zhí)行一次,內(nèi)循環(huán)執(zhí)行N次。
工具:
電腦軟件
for循環(huán)
三角形
方法:
1、打印定點(diǎn)在左下的直角三角形;
2、打印定點(diǎn)在左上的直角三角形;
3、打印定點(diǎn)在右下的直角三角形,這里三角形的形狀是由*所占的位置決定的;
4、打印定點(diǎn)在左下的直角三角形,這里三角形的形狀是由*所占的位置決定的;
5、打印定點(diǎn)在正上方的直角三角形;
6、打印定點(diǎn)在正下方的直角三角形。
一般來說.比較常使用的是?JTextArea文本框的無參數(shù)構(gòu)造器進(jìn)行構(gòu)造
然后可以使用setText("文字")來設(shè)置文本,或者getText()方法來獲得文本
JTextArea?jta?=?new?JTextArea();
jta.setText("你好啊");
String?txt?=?jta.getText();
具體例子
import?java.awt.*;
import?java.awt.event.*;
import?java.io.*;
import?javax.swing.*;
public?class?ReadBook?extends?JFrame{
JTextArea?jta;
JTextField?jtf;
JButton?jb;
public?ReadBook(){
jta?=?new?JTextArea();
jtf?=?new?JTextField(30);
jtf.setText("文件路徑如c:\\ab.txt");
jb?=?new?JButton("顯示文字");
JPanel?jp?=?new?JPanel();
jp.add(jtf);
jp.add(jb);
add(jta);
add(jp,BorderLayout.SOUTH);
setBounds(500,?100,?500,?380);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
jb.addActionListener(new?ActionListener()?{
@Override
public?void?actionPerformed(ActionEvent?e)?{
String?path?=?jtf.getText();
File?f=?new?File(path);
if(f.exists()!f.isDirectory()){//存在該文件且不是文件夾
try?{
StringBuilder?sb?=?new?StringBuilder();
BufferedReader?br?=?new?BufferedReader(new?FileReader(f));
String?temp;
while((temp=br.readLine())!=null){
sb.append(temp+"\n");
}
br.close();
jta.setText(sb.toString());//setText方法可以給jta添加文字
}?catch?(Exception?e1)?{
e1.printStackTrace();
}
}else{
jtf.setText("路徑錯(cuò)誤.重新填寫");
}
}
});
}
public?static?void?main(String[]?args)?{
new?ReadBook();
}
}
效果
不好意思,臨睡覺才想起來,這個(gè)是完整的程序,你建一個(gè)名字是ShapeTest的類,把這段代碼復(fù)制進(jìn)去就能運(yùn)行了,要是sysout方法里有亂碼,把字符類型改成UTF-8就行了。
abstract class Shape {
public double x;
public double y;
public double getX(){
return this.x=x;
}
public double getY(){
return this.y=y;
}
abstract double getArea(double x, double y);
}
class Circle extends Shape{
@Override
double getArea(double r, double PI) {
return PI*r*r;
}
}
class Rectangle extends Shape{
@Override
double getArea(double x, double y) {
return x*y;
}
}
class Square extends Shape{
@Override
double getArea(double x,double y) {
return x*y;
}
}
public class ShapeTest{
public static void main(String args[]){
double r=2;
double x=3;
double y=4;
final double PI=3.14;
Circle circle=new Circle();
Rectangle rectangle=new Rectangle();
Square square=new Square();
double circleArea=circle.getArea(r, PI);
double rectangleArea=rectangle.getArea(x, y);
double squareArea=square.getArea(x, y);
System.out.println("圓的面積是:"+circleArea);
System.out.println("矩形的面積是:"+rectangleArea);
System.out.println("正方形的面積是:"+squareArea);
}
}
用java代碼輸出心的形狀
public class CartesianCurve extends Applet{
int width, height;
Image image;
Graphics draw_Curve;
public void init(){
setBackground(Color.black);
this.setSize(350, 310);
width = getSize().width;
height = getSize().height;
image = createImage(width, height);
draw_Curve = image.getGraphics();
}
public void paint(Graphics g){
draw_Curve.clearRect(0, 0, width, height);
draw_Curve.setColor(Color.red);
int i, j;
double x, y, r;
for(i = 0; i = 90; i++)
for(j = 0; j = 90; j++){
r = Math.PI / 45 * i * (1 - Math.sin(Math.PI / 45 * j)) * 18;
x = r * Math.cos(Math.PI / 45 * j) * Math.sin(Math.PI / 45 * i) + width / 2;
y = -r * Math.sin(Math.PI / 45 * j) + height / 4;
draw_Curve.fillOval((int) x, (int)y, 2, 2);
}
g.drawImage(image, 0, 0, this);
}
}