下面是一個可能的Java源代碼,它包含了一個接口(Shape)和五個類(Circle, Rectangle, Triangle, Square 和 Main)。它的功能是計算不同形狀的面積和周長。
10年積累的成都網(wǎng)站建設(shè)、做網(wǎng)站經(jīng)驗,可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識你,你也不認(rèn)識我。但先網(wǎng)站設(shè)計后付款的網(wǎng)站建設(shè)流程,更有裕華免費網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
//定義一個接口Shape,有兩個抽象方法:getArea()和getPerimeter()interface Shape { double getArea(); double getPerimeter();
}//定義一個類Circle,實現(xiàn)Shape接口class Circle implements Shape { //定義一個私有屬性radius,表示圓的半徑
private double radius; //定義一個公有構(gòu)造方法,用于初始化radius
public Circle(double radius) { this.radius = radius;
} //實現(xiàn)getArea()方法,返回圓的面積
public double getArea() { return Math.PI * radius * radius;
} //實現(xiàn)getPerimeter()方法,返回圓的周長
public double getPerimeter() { return Math.PI * radius * 2;
}
}//定義一個類Rectangle,實現(xiàn)Shape接口class Rectangle implements Shape { //定義兩個私有屬性width和height,表示矩形的寬度和高度
private double width; private double height; //定義一個公有構(gòu)造方法,用于初始化width和height
public Rectangle(double width, double height) { this.width = width; this.height = height;
} //實現(xiàn)getArea()方法,返回矩形的面積
public double getArea() { return width * height;
} //實現(xiàn)getPerimeter()方法,返回矩形的周長
public double getPerimeter() { return (width + height) *2;
}
}//定義一個類Triangle,實現(xiàn)Shape接口class Triangle implements Shape { //定義三個私有屬性a,b,c表示三角形的三條邊長
private double a; private double b; private double c; //定義一個公有構(gòu)造方法,用于初始化a,b,c,并檢查是否滿足三角形條件(任意兩邊之和大于第三邊)
public Triangle(double a, double b, double c) throws Exception{ if (a + b c a + c b b + c a) {
this.a = a; this.b = b;
this.c = c;
} else {
throw new Exception("Invalid triangle");
}
} //實現(xiàn)getArea()方法,返回三角形的面積(使用海倫公式)
public double getArea() { //計算半周長p
double p = (a + b + c) /2; //計算并返回面積s(使用Math.sqrt()函數(shù)求平方根)
return Math.sqrt(p * (p - a) * (p - b) * (p - c));
} //實現(xiàn)getPerimeter()方法,返回三角形的周長
public double getPerimeter(){ return a + b + c;
}
}//定義一個類Square,繼承Rectangle類,并重寫構(gòu)造方法和toString()方法class Square extends Rectangle { //重寫構(gòu)造方法,在調(diào)用父類構(gòu)造方法時傳入相同的參數(shù)side作為width和height
public Square(double side){ super(side, side);
} //重寫toString()方法,在原來基礎(chǔ)上加上"Square:"前綴,并只顯示side屬性而不顯示width和height屬性(使用String.format()函數(shù)格式化字符串)
@Override
public String toString(){ return String.format("Square: side=%.2f", super.width); /* 或者直接使用super.getPerimeter()/4作為side */
/* return String.format("Square: side=%.2f", super.getPerimeter()/4); */
/* 注意:不能直接訪問super.side屬性,
package?test2;
import?java.io.BufferedReader;
import?java.io.File;
import?java.io.FileInputStream;
import?java.io.FileOutputStream;
import?java.io.IOException;
import?java.io.InputStream;
import?java.io.InputStreamReader;
import?java.util.HashMap;
import?java.util.Map;
import?java.util.Set;
public?class?JavaCodeAnalyzer?{
public?static?void?analyze(File?file)?throws?IOException{
//FileOutputStream?fos?=?new?FileOutputStream("F;"+File.separator+"result.txt");
if(!(file.getName().endsWith(".txt")||file.getName().endsWith(".java"))){
System.out.println("輸入的分析文件格式不對!");
}
InputStream?is=?new?FileInputStream(file);
BufferedReader?br=?new?BufferedReader(new?InputStreamReader(is));
String?temp;
int?count=0;
int?countSpace=0;
int?countCode=0;
int?countDesc=0;
MapString,?Integer?map?=?getKeyWords();
while((temp=br.readLine())!=null){
countKeys(temp,?map);
count++;
if(temp.trim().equals("")){
countSpace++;
}else?if(temp.trim().startsWith("/*")||temp.trim().startsWith("http://")){
countDesc++;
}else{
countCode++;
}
}
System.out.printf("代碼行數(shù):"+countCode+"占總行數(shù)的%4.2f\n",(double)countCode/count);
System.out.printf("空行數(shù):"+countSpace+"占總行數(shù)的%4.2f\n",(double)countSpace/count);
System.out.printf("注釋行數(shù):"+countDesc+"占總行數(shù)的%4.2f\n",(double)countDesc/count);
System.out.println("總行數(shù):"+count);
System.out.println("出現(xiàn)最多的5個關(guān)鍵字是:");
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("");
}
public?static?void?main(String[]?args)?{
getKeyWords();
File?file?=?new?File("F://Test.java");
try?{
analyze(file);
}?catch?(IOException?e)?{
//?TODO?自動生成?catch?塊
e.printStackTrace();
}
}
public?static?MapString,Integer?getKeyWords(){
MapString,Integer?map?=?new?HashMapString,?Integer();
String[]keywords?=?{"abstract","assert","boolean","break","byte","case","catch","char","class","continue","default","do","double","else","enum","extends","final","finally","float","for","if","implements","import","instanceof","int","interface","long","native","new","package","private","protected","public","return","????strictfp","short","static","super","????switch","synchronized","this","throw","throws","transient","try","void","volatile","while","goto","const"};
for(String?s:keywords){
map.put(s,?0);
}
return?map;
}
public?static?void?countKeys(String?s,MapString,Integer?map){
SetString?keys?=?map.keySet();
for(String?ss:keys){
if(s.indexOf(ss)!=-1){
map.put(ss,?map.get(ss)+1);
}
}
}
}
上班沒啥時間了,還有點沒寫完,你在想想。
你是青鳥的吧?這我寫過?有源碼?這里怎么上傳壓縮包啊
package?ghhh;
import?java.util.Scanner;
public?class?DvD?{
public?static?void?main(String[]?args)?{
int?state[]=new??int[6];
String?name[]=new??String[6];
int?date[]=new?int[6];
int?count[]=new?int?[6];
name[0]="權(quán)利的游戲";
name[1]="命運之夜";
name[2]="傲慢與偏見";
state[0]=1;
state[1]=0;
state[2]=1;
date[0]=13;
date[1]=0;
date[2]=9;
count[0]=23;
count[1]=23;
count[2]=23;
int?n;
// boolean?n=false;
do{
System.out.println("歡迎使用迷你DVD管理器");
System.out.println("1.新增DVD");
System.out.println("2.查看DVD");
System.out.println("3.刪除DVD");
System.out.println("4.借出DVD");
System.out.println("5.歸還DVD");
System.out.println("6.退出DVD");
Scanner?input?=new?Scanner(System.in);
System.out.println("請選擇:");
?n=input.nextInt();
switch(n){
case?1:
System.out.println("請輸入要增加DVD的名稱:");
String?name1=input.next();
boolean?flag=false;
for(int?i=0;iname.length;i++){
if(name[i]==null){
name[i]=name1;
flag=true;
break;
}
}
if(flag){
System.out.println("新增DVD"+name1+"成功");
}else{
System.out.println("貨架已滿!增加失?。?);
}
System.out.println("請輸入0返回!");
n=input.nextInt();
break;
case?2:
System.out.println("序號\t"+"狀態(tài)\t"+"名稱\t\t"+"借出日期\t"+"借出次數(shù)");
for(int?i=0;iname.length;i++){
if(name[i]!=null){
String?state1?=((state[i]==0)?"可借":"已借");
String?date1=((date[i]==0)?"":date[i]+"日");
String?count1=count[i]+"次";
System.out.println((i+1)+"\t"+state1+"\t"+name[i]+"\t"+date1+"\t\t"+count1);
}
}
System.out.println("請輸入0返回!");
n=input.nextInt();
break;
case?3:
System.out.println("請輸入要刪除的DVD名稱:");
String?name2=input.next();
int?index=-1;
boolean?a=false;
boolean?flag1=false;
for(int?i=0;iname.length;i++){
if(name2.equals(name[i])state[i]==1){
System.out.println("此DVD已經(jīng)借出,無法刪除");
a=true;
break;
}else?if(name2.equals(name[i])state[i]==0){
a=true;
index=i;
flag1=true;
System.out.println("刪除成功!");
break;
}
}
if(a==false){
System.out.println("沒有找到相同名稱的DVD!");
}
if(flag1){
for?(int?i=index;iname.length;i++){
if(i!=name.length-1){
name[i]=name[i+1];
state[i]=state[i+1];
date[i]=date[i+1];
count[i]=count[i+1];
}
name[name.length-1]=null;
state[name.length-1]=0;
date[name.length-1]=0;
count[name.length-1]=0;
}
}
System.out.println("請輸入0返回!");
n=input.nextInt();
break;
case?4:
System.out.println("請輸入要借出的DVD:");
?String?name3=input.next();
?boolean?a3=false;
?boolean?b3=false;
?for(int?i=0;iname.length;i++){
?if(name3.equals(name[i])??state[i]==1){
?System.out.println("該DVD已經(jīng)借出");
?a3=true;
?}else?if(name3.equals(name[i])??state[i]==0){
?do{
?System.out.println("請輸入借出的日期:");
int?m=input.nextInt();
?
?if(m31||m1){
?System.out.println("請重新輸入日期:");
?b3=true;
?}else{
?date[i]=m;?
?state[i]=1;
?count[i]+=1;
?}
?
?}while(b3==true);
?System.out.println("借出成功!");
?a3=true;
?}
?}
?if(a3==false){
?System.out.println("沒有該DVD");
?}
?System.out.println("請輸入0返回!");
?n=input.nextInt();
break;
case?5:
System.out.println("請輸入要歸還的DVD:");
String?name5=input.next();
boolean?b5=false;
boolean?m5=false;
for(int?i=0;iname.length;i++){
if(name5.equals(name[i])??state[i]==1){
b5=true;
do{
System.out.println("請輸入要歸還DVD的日期:(歸還日期請輸入當(dāng)月日期?1~31)");
int?a5=input.nextInt();
if(a531){
System.out.println("請重新輸入日期:");
m5=true;
}else?if(a5date[i]){
System.out.println("借出日期是"+date[i]+"日\t輸入的日期不能小于借出的日期,請重新輸入日期:");
m5=true;
}else{
state[i]=0;
System.out.println("歸還成功");
System.out.println("借出日期是:"+date[i]+"歸還日期是:"+a5+"日\t租金一天一元:共"+(a5-date[i])+"元");
???date[i]=0;
???m5=false;
}
}while(m5==true);
}else?if?(name5.equals(name[i])??state[i]==0){
System.out.println("該DVD未借出,不可歸還!");
b5=true;
}
}
if(b5==false){
System.out.println("沒有該名稱的DVDV");
}
System.out.println("請輸入0返回!");
?n=input.nextInt();
break;
case?6:
n=1;
System.out.println("程序退出!");
break;
default:
if(n==0){
}else{
System.out.println("輸入錯誤!請重新輸入!");
n=0;
}
break;
}
}while(n==0);
System.out.println("謝謝使用!");
}
}
看看有沒有問題 好久之前的了
給你個做好了的Java的源程序的記事本,自己看看就行了的,不怎么難的···
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class MyNotepad implements ActionListener{
private JFrame frame=new JFrame("新記事本");
private JTextArea jta=new JTextArea();
private String result="";
private boolean flag=true;
private File f;
private JButton jb=new JButton("開始");
private JTextField jtf=new JTextField(15);
private JTextField jt=new JTextField(15);
private JButton jbt=new JButton("替換為");
private JButton jba=new JButton("全部替換");
private Icon ic=new ImageIcon("D:\\java課堂筆記\\GUI\\11.gif");
private String value;
private int start=0;
private JFrame jf=new JFrame("查找");
private JFrame jfc=new JFrame("替換");
@Override
public void actionPerformed(ActionEvent e) {
String comm=e.getActionCommand();
if("新建".equals(comm)){
if(!(frame.getTitle().equals("新記事本"))){
if(!flag){
write();
newNew();
}else{
JFileChooser jfc=new JFileChooser("D:\\java課堂筆記");
int returnVal = jfc.showDialog(null,"保存為");
if(returnVal == JFileChooser.APPROVE_OPTION) {//選擇文件后再執(zhí)行下面的語句,保證了程序的健壯性
f=jfc.getSelectedFile();
flag=false;
write();
}
}
}else if(!(jta.getText().isEmpty())){
JFileChooser jfc=new JFileChooser("D:\\java課堂筆記");
int returnVal = jfc.showDialog(null,"保存為");
if(returnVal == JFileChooser.APPROVE_OPTION) {//選擇文件后再執(zhí)行下面的語句,保證了程序的健壯性
f=jfc.getSelectedFile();
flag=false;
write();
newNew();
}
}else{
newNew();
}
}else if("打開".equals(comm)){
JFileChooser jfc=new JFileChooser("D:\\java課堂筆記");
jfc.setDialogType(JFileChooser.OPEN_DIALOG);
int returnVal = jfc.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION) {//選擇文件后再執(zhí)行下面的語句,保證了程序的健壯性
f=jfc.getSelectedFile();
frame.setTitle(f.getName());
result=read();
flag=false;
value=result;
jta.setText(result);
}
}else if("保存".equals(comm)){
JFileChooser jfc=new JFileChooser("D:\\java課堂筆記");
if(flag){
int returnVal = jfc.showDialog(null,"保存為");
if(returnVal == JFileChooser.APPROVE_OPTION) {//選擇文件后再執(zhí)行下面的語句,保證了程序的健壯性
f=jfc.getSelectedFile();
flag=false;
write();
}
}else{
write();
}
}else if("另存".equals(comm)){
JFileChooser jfc=new JFileChooser("D:\\java課堂筆記");
int returnVal = jfc.showDialog(null,"另存");
if(returnVal == JFileChooser.APPROVE_OPTION) {//選擇文件后再執(zhí)行下面的語句,保證了程序的健壯性
f=jfc.getSelectedFile();
write();
}
}else if("退出".equals(comm)){
System.exit(0);
}else if("撤銷".equals(comm)){
jta.setText(value);
}else if("剪切".equals(comm)){
value=jta.getText();
jta.cut();
}else if("復(fù)制".equals(comm)){
jta.copy();
}else if("粘貼".equals(comm)){
value=jta.getText();
jta.paste();
}else if("刪除".equals(comm)){
value=jta.getText();
jta.replaceSelection(null);
}else if("全選".equals(comm)){
jta.selectAll();
}else if("查找".equals(comm)){
value=jta.getText();
jf.add(jtf,BorderLayout.CENTER);
jf.add(jb,BorderLayout.SOUTH);
jf.setLocation(300,300);
jf.pack();
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}else if("替換".equals(comm)){
value=jta.getText();
GridLayout gl=new GridLayout(3,3);
JLabel jl1=new JLabel("查找內(nèi)容:");
JLabel jl2=new JLabel("替換為:");
jfc.setLayout(gl);
jfc.add(jl1);
jfc.add(jtf);
jfc.add(jb);
jfc.add(jl2);
jfc.add(jt);
jfc.add(jbt);
JLabel jl3=new JLabel();
JLabel jl4=new JLabel();
jfc.add(jl3);
jfc.add(jl4);
jfc.add(jba);
jfc.setLocation(300,300);
jfc.pack();
jfc.setVisible(true);
jfc.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}else if("版本".equals(comm)){
JDialog jd=new JDialog(frame,"關(guān)于對話框");
jd.setSize(200,200);
JLabel l=new JLabel("哈哈哈哈哈哈哈哈哈哈呵呵呵呵呵呵呵呵呵呵呵呵呵");
jd.add(l,BorderLayout.CENTER);
jd.setLocation(100,200);
jd.setSize(300,300);
jd.setVisible(true);
// jd.pack();
jd.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
}else if("開始".equals(comm)||"下一個".equals(comm)){
String temp=jtf.getText();
int s=value.indexOf(temp,start);
if(value.indexOf(temp,start)!=-1){
jta.setSelectionStart(s);
jta.setSelectionEnd(s+temp.length());
jta.setSelectedTextColor(Color.GREEN);
start=s+1;
jb.setText("下一個");
// value=value.substring(s+temp.length());//不能截取字串
}else {
JOptionPane.showMessageDialog(jf, "查找完畢!", "提示", 0, ic);
jf.dispose();
}
}else if("替換為".equals(comm)){
String temp=jtf.getText();
int s=value.indexOf(temp,start);
if(value.indexOf(temp,start)!=-1){
jta.setSelectionStart(s);
jta.setSelectionEnd(s+temp.length());
jta.setSelectedTextColor(Color.GREEN);
start=s+1;
jta.replaceSelection(jt.getText());
}else {
JOptionPane.showMessageDialog(jf, "查找完畢!", "提示", 0, ic);
jf.dispose();
}
}else if("全部替換".equals(comm)){
String temp=jta.getText();
temp=temp.replaceAll(jtf.getText(), jt.getText());
jta.setText(temp);
}
}
public String read(){
String temp="";
try {
FileInputStream fis = new FileInputStream(f.getAbsolutePath());
byte[] b=new byte[1024];
while(true){
int num=fis.read(b);
if(num==-1)break;
temp=temp+new String(b,0,num);
}
fis.close();
} catch (Exception e1) {
e1.printStackTrace();
}
return temp;
}
public void write(){
try {
FileOutputStream fos=new FileOutputStream(f);
fos.write(jta.getText().getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void newNew(){
frame.dispose();
new MyNotepad();
flag=true;
}
public MyNotepad(){
JMenuBar jmb=new JMenuBar();
String[] menuLab={"文件","編輯","幫助"};
String[][] menuItemLab={{"新建","打開","保存","另存","退出"},
{"撤銷","剪切","復(fù)制","粘貼","刪除","全選","查找","替換"},
{"版本"}};
for(int i=0;imenuLab.length;i++){
JMenu menu=new JMenu(menuLab[i]);
jmb.add(menu);
for(int j=0;jmenuItemLab[i].length;j++){
JMenuItem jmi=new JMenuItem(menuItemLab[i][j]);
menu.add(jmi);
jmi.addActionListener(this);
}
}
frame.setJMenuBar(jmb);
jta.setLineWrap(true);//自動換行
JScrollPane jsp=new JScrollPane(jta);//滾動窗口面板
frame.add(jsp);
jb.addActionListener(this);
jbt.addActionListener(this);
jba.addActionListener(this);
frame.setLocation(200,50);
frame.setSize(620,660);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new MyNotepad();
}
}