java代碼案例-ag真人国际官网
ⅰ java100行以上源代碼,至少五個class以及一個interface,可以簡單點
下面是一個可能的java源代碼,它包含了一個介面租沖薯(shape)和五個類(circle, rectangle, triangle, square 和 main)。它的功能是計算不同形狀的面積和周長。
//定義一個介面shape,有兩判指個抽象方法:getarea()和getperimeter()interface shape { double getarea(); double getperimeter();
}//定義一個類circle,實現shape介面class circle implements shape { //定義一個私有屬性radius,表示圓的半徑
private double radius; //定義一個公有構造方法,用於初始化radius
public circle(double radius) { this.radius = radius;
} //實現getarea()方法,返回圓的面積
public double getarea() { return math.pi * radius * radius;
} //實現getperimeter()方法,返回圓的周長
public double getperimeter() { return math.pi * radius * 2;
}
}//定義一個類rectangle,實現shape介面class rectangle implements shape { //定義兩個私有屬性width和height,表示矩形的寬度和高度
private double width; private double height; //定義一個公有構造方法,用於初始化width和height
public rectangle(double width, double height) { this.width = width; this.height = height;
} //實現getarea()方法,返回矩形的面積
public double getarea() { return width * height;
} //實現getperimeter()方法,返回矩形的周長
public double getperimeter() { return (width height) *2;
}
}//定義一個類triangle,實現shape介面class triangle implements shape { //定義三個私有屬性a,b,c表示三角形的三條邊長
private double a; private double b; private double c; //定義一個公有構造方法,用於初始化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");
}
} //實現getarea()方法,返回三角形的面積(使用海倫公式)
public double getarea() { //計算半周長p
double p = (a b c) /2; //計算並返回面積s(使用math.sqrt()函數求平方根)
return math.sqrt(p * (p - a) * (p - b) * (p - c));
} //實現getperimeter()方法,返回三角形的周長
public double getperimeter(){ return a b c;
}
}//定義一個類square,繼承rectangle類,並重寫構造方法和tostring()方法class square extends rectangle { //重寫構造方法,在調用父類構造方法時傳入相弊者同的參數side作為width和height
public square(double side){ super(side, side);
} //重寫tostring()方法,在原來基礎上加上"square:"前綴,並只顯示side屬性而不顯示width和height屬性(使用string.format()函數格式化字元串)
@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屬性,
ⅱ 給段最簡單的java代碼 讓我新手看一下
最簡單的java代碼肯定就是這個了,如下:
public class myfirstapp
{
public static void main(string[] args)
{
system.out.print("hello world");
}
}
「hello world」就是應該是所有學java的新手看的第一個代碼了。如果是零基礎的新手朋友們可以來我們的java實驗班試聽,有免費的試聽課程幫助學習java必備基礎知識,有助教老師為零基礎的人提供個人學習方案,學習完成後有考評團進行專業測試,幫助測評學員是否適合繼續學習java,15天內免費幫助來報名體驗實驗班的新手快速入門java,更好的學習java!
ⅲ 求java工廠模式的一個簡單代碼例子,盡量簡單
這個應該比較簡單一點。
某系統日誌記錄器要求支持多種日誌記錄方式,如文件記錄、資料庫記錄等,且用戶可以根據要求動態選擇日誌記錄方式。現使用工廠方法模式設計該系統,並寫出相應java代碼。
interface log{
public void writelog();
}
class filelog implements log{
public void writelog(){
system.out.println("文件記錄");
}
}
class databaselog implements log{
public void writelog(){
system.out.println("資料庫記錄");
}
}
interface logfactory{
public log createlog();
}
class filelogfactory implements logfactory{
public log createlog(){
return new filelog();
}
}
class databaselogfactory implements logfactory{
public log createlog(){
return new databaselog();
}
}
public class client{
public static void main(string[] args) {
try{
log log;
logfactory factory;
//這里可以改成使用dom和java反射機制讀取xml文件,獲取工廠類名
factory=new databaselogfactory ();
log=factory.createlog();
log.writelog();
}
catch(exception e){
system.out.println(e.getmessage());
}
}
}
ⅳ 一個簡單的java程序代碼
public double getcost(int minutes)
{
//整數時間所花的費用
int aa = minutes / 60; //未滿1小時處理
if (minutes < 60)
return 2;
//超出小時部分
int bb = minutes % 60; //其實你還有必要做一些其他處理。比如說超過30分鍾了該怎麼樣算等等...... return aa * 2 bb * 0.01d;
}