(1.定義一個(gè)ReList接口,并使用數(shù)組的方法來實(shí)現(xiàn)接口。
要求:
1. ReList接口包含六個(gè)方法:
void add(Object obj):往列表尾部添加對(duì)象
Object get(int index):從列表中獲取索引為i的對(duì)象
void clear():清空所有的對(duì)象
boolean isEmpty():判斷列表中是否有對(duì)象
int size():獲取列表中對(duì)象的個(gè)數(shù)
int capacity():所分配的空間大小
2. ReArrayList類實(shí)現(xiàn)ReList接口裁良,內(nèi)部以數(shù)組的方式實(shí)現(xiàn),要求:
構(gòu)造函數(shù)ReArrayList(int incSize):參數(shù)incSize為數(shù)組初始化大小和空間的增量隙赁。若用戶調(diào)用incSize非法杖狼,則設(shè)為默認(rèn)值5。
當(dāng)調(diào)用add()方法往試圖往ReArrayList中增加對(duì)象時(shí)猿挚,如果內(nèi)部數(shù)組已滿咐旧,則增加數(shù)組大小,增量為incSize绩蜻。
調(diào)用clear()方法可以清空所有通過add()方法加入的對(duì)象铣墨。
調(diào)用get()方法時(shí),如果傳入的參數(shù)非法办绝,則返回null對(duì)象伊约,并使用System.err提示用戶參數(shù)錯(cuò)誤。
3. ReDoubledArrayList類也實(shí)現(xiàn)ReList接口孕蝉,內(nèi)部依舊通過數(shù)組實(shí)現(xiàn)屡律,要求:
構(gòu)造函數(shù)ReDoubledArrayList(int initSize):參數(shù)initSize表示數(shù)組的初始化大小。若用戶調(diào)用initSize非法昔驱,則設(shè)為默認(rèn)值10疹尾。
當(dāng)調(diào)用add()方法往ReDoubledArrayList列表里面增加對(duì)象時(shí),如果其內(nèi)部數(shù)組已滿骤肛,則將數(shù)組的長(zhǎng)度變?yōu)楫?dāng)前長(zhǎng)度的2倍纳本。
其他方法和ReArrayList一致
最后設(shè)計(jì)一個(gè)類來測(cè)試你的ReArrayList和ReDoubledArrayList類,看看這兩個(gè)類是否能在不破壞使用者代碼的情況下相互替換腋颠。
提示:
1. 增加數(shù)組長(zhǎng)度的方法:使用java.util.Arrays.copyOf()方法繁成,用法請(qǐng)查閱Java API文檔。
Stuednt.java
package shiyan3_4;
public class Stuednt {
?????? publicString toString() {
????????????? return "Stuednt [name=" + name + ",
age=" + age + "]";
?????? }
?????? privateString name;
?????? private int age;
?????? publicStuednt(String name,int age) {
????????????? this.name=name;
????????????? this.age=age;
?????? }
?????? publicString getName() {
????????????? return name;
?????? }
?????? public void setName(String name) {
????????????? this.name = name;
?????? }
?????? public int getAge() {
????????????? return age;
?????? }
?????? public void setAge(int age) {
????????????? this.age = age;
?????? }
}
ReList.java
package shiyan3_4;
public interface ReList {
?public void add(Object obj);
?public Object get(int index);
?public void clear();
?public booleanisEmpty();
?public int size();
?public int capacity();
}
ReArrayList.java
package shiyan3_4;
import java.util.Arrays;
public class ReArrayList implementsReList{
private int size;
private Object[] example;
?????? publicReArrayList(int incSize) {
????????????? if(incSize<0){
???????????????????? this.size=5;
????????????? }else{
???????????????????? this.size=incSize;
????????????? }
????????????? this.example = new Object[size];
????????????? System.out.println("數(shù)組初始化成功淑玫,增量為=:"+size);
?????? }
?????? public void add(Object obj) {
?????? System.out.println("執(zhí)行add函數(shù)巾腕,添加元素:"+obj);
?????? for(int i=0;i<example.length;i++){
????????????? if(example[i]==null){
???????????????????? example[i]=obj;
???????????????????? return;
????????????? }
?????? }
?????? System.out.println("數(shù)組已滿面睛,增加容量"+size);
?????? this.example= Arrays.copyOf(example,? example.length+size);
?????? System.out.println("新數(shù)組長(zhǎng)度為:="+example.length);
?????? System.out.println("數(shù)組內(nèi)部元素:="+this);/*那么?System.out.println(this)?將打印A類自身引用的toString方法, 如果自身沒有實(shí)現(xiàn)尊搬,則打印的是父類Object的toString方法叁鉴;getClass().getName()+
"@" +Integer.toHexString(hashCode()); ?打印的是“類名@hascode”*/
????????????? this.add(obj);
?????? }
?????? publicObject get(int index) {
?????? System.out.println("執(zhí)行查找指定腳標(biāo)元素:");
?????? if(index>=example.length){
????????????? System.err.println("參數(shù)錯(cuò)誤!"+index);
????????????? return null;
?????? }
????????????? return example[index];
?????? }
?????? public void clear() {
?????? this.example = new Object[size];
?????? System.out.println("數(shù)組已清空,當(dāng)前數(shù)組長(zhǎng)度為:="+example.length);
?????? }
?????? public boolean isEmpty() {
????????????? System.out.println("isEmpty判斷非空佛寿,數(shù)組元素:"+this);
????????????? for(Objecto : example){
???????????????????? if(o!=null)
????????????? return false;
????????????? }
????????????? return true;
?????? }
?????? public int size() {
????????????? System.out.println("數(shù)組大小為:=");
????????????? return (int)Arrays.stream(example).filter(e->e!=null).count();//過濾
?????? }
?????? public int capacity() {
????????????? System.out.println("分配的大小為:=");
????????????? return example.length;
?????? }
?????? publicString toString() {
????????????? return "ReArrayList [size=" + size + ",
example="+ Arrays.toString(example) + "]";
?????? }
}
Ceshi.java
package shiyan3_4;
public class ceshi {
?????? public static void main(String[] args) {
????????????? ReArrayListar = new ReArrayList(2);
????????????? ar.add(new Stuednt("小紅",14));
????????????? ar.add(new Stuednt("小白",17));
????????????? ar.add(new Stuednt("小黃",11));
????????????? ar.add(new Stuednt("小藍(lán)",13));
????????????? ar.add(new Stuednt("小青",14));
????????????? ar.add(new Stuednt("小紫",12));
????????????? System.out.println(ar);
????????????? System.out.println(ar.get(3));
????????????? System.out.println(ar.size());
????????????? System.out.println(ar.isEmpty());
????????????? System.out.println(ar.capacity());
????????????? ar.clear();
????????????? System.out.println(ar);
????????????? System.out.println(ar.size());
????????????? System.out.println(ar.isEmpty());
????????????? System.out.println(ar.capacity());
?????? }
}
(2.假設(shè)默認(rèn)長(zhǎng)度單位為米幌墓,編寫一個(gè)計(jì)算為各種形狀著色花費(fèi)的程序。
要求:
1. 編寫B(tài)orderColorable接口冀泻,要求:
具有方法void paintBorder()常侣,在實(shí)現(xiàn)類中該方法需要輸出“已經(jīng)將什么形狀的邊著色”。
方法boolean isBorderPainted()弹渔,返回形狀的邊是否已經(jīng)著色胳施。
2. 編寫SolidColorable接口,要求:
具有方法void paintShape()肢专,在實(shí)現(xiàn)類中該方法需要輸出“已經(jīng)將什么形狀進(jìn)行著色”舞肆。
方法boolean isShapePainted(),返回形狀是否已經(jīng)著色胆绊。
3. Shape2D是所有形狀的父類:
其中圓(Circle)氨鹏,三角形(Triangle)和長(zhǎng)方形(Rectangle)均繼承自Shape2D類欧募,而Square繼承自Rectangle類。
Circle實(shí)現(xiàn)了BorderColorable接口仆抵,Triangle實(shí)現(xiàn)了SolidColorable接口跟继,而Rectangle同時(shí)實(shí)現(xiàn)了BorderColorable接口和SolidColorable接口。
4. CostCalculator類用來計(jì)算為邊著色和為形狀著色所消耗的花費(fèi)镣丑。
構(gòu)造函數(shù)CostCalculator(double
borderCost, double solidCost)舔糖,其中borderCost表示畫1米的邊需要的單價(jià),solidCost表示畫一平方米的邊需要消耗的單價(jià)莺匠。
double calculate(Shape2D shape)用來計(jì)算實(shí)際為形狀shape著色需要的花費(fèi)金吗。
形狀著色的花費(fèi)計(jì)算值計(jì)算已經(jīng)著色的部分,且花費(fèi)總額為邊著色和為形狀著色的花費(fèi)的總和趣竣。
5. 寫一個(gè)測(cè)試類測(cè)試你的程序摇庙。
提示:
1. 實(shí)現(xiàn)了BorderColorable的形狀只為邊著色,實(shí)現(xiàn)了SolidColorable的形狀為整個(gè)形狀著色遥缕。
2. 總體類圖如下圖所示卫袒。
BorderColorable.java
package shiyan4_2;
public interface BorderColorable {
public void paintBorder();
public boolean isShapeBorder();
}
Circle.java
package shiyan4_2;
public class Circle extendsShape2D implements BorderColorable{
?????? private double r;
?????? private boolean flag1 = false;
?????? private boolean flag2 = false;
?????? private double Area;
?????? private double Perimeter;
?????? public double getArea() {
????????????? return Area;
?????? }
?????? public void setArea(double area) {
????????????? Area = area;
?????? }
?????? public double getPerimeter() {
????????????? return Perimeter;
?????? }
?????? public void setPerimeter(double perimeter) {
????????????? Perimeter = perimeter;
?????? }
?????? publicCircle(Double radius) {
????????????? r=radius;
?????? }
? public double getR() {
????????????? return r;
?????? }
?????? public void setR(double r) {
????????????? this.r = r;
?????? }
?????? public void area() {
????????????? double PI =
(float) Math.PI;
??? System.out.println("圓的面積為:"+PI*getR()*getR()
);
??? setArea(PI*getR()*getR());
}
public void perimeter() {
?????? double PI =(float) Math.PI;
??? System.out.println("圓的周長(zhǎng)為:"+2*PI*getR());
??? setPerimeter(2*PI*getR());
}
?????? public void paintBorder(){
????????????? flag1=true;
????????????? System.out.println("正在為圓形的邊著色~~");
?????? }
?????? public void paintShape(){
????????????? flag2=true;
????????????? System.out.println("正在為圓形的內(nèi)部著色~~");
?????? }
?????? public boolean isShapeBorder() {
??? if (flag1)
??????? return true;
??? else
??????? return false;
}
?????? public boolean isShapePainted() {
??? if (flag2)
??????? return true;
??? else
??????? return false;
}
}
CostCalculator.java
package shiyan4_2;
public class CostCalculator {
?????? private double borderCost,soliderCost;
public double getBorderCost() {
????????????? return borderCost;
?????? }
?????? public void setBorderCost(double borderCost) {
????????????? this.borderCost = borderCost;
?????? }
?????? public double getSoliderCost() {
????????????? return soliderCost;
?????? }
?????? public void setSolidCost(double solidCost) {
????????????? this.soliderCost = solidCost;
?????? }
public CostCalculator(double borderCost, double solidCost) {
?????? this.borderCost=borderCost;
?????? this.soliderCost=solidCost;
}
public double calculate(Shape2D shape){
?????? double s1=0,s2=0;
?????? if(shape.isShapeBorder()){
????????????? s1=shape.getPerimeter()*this.borderCost;
?????? }
?????? if(shape.isShapePainted()){
????????????? s2=shape.getArea()*this.soliderCost;
?????? }
?????? return s1+s2;
}
}
Rectangle.java
package shiyan4_2;
public class Rectangle extendsShape2D implements SoliderColorable ,BorderColorable{
?????? private boolean flag1 = false;
?????? private boolean flag2 = false;
?????? private double Area;
?????? private double Perimeter;
?????? private double l,h;
?????? publicRectangle(double l,double h) {
????????????? this.h=h;
????????????? this.l=l;
?????? }
?????? public double getL() {
????????????? return l;
?????? }
?????? public void setL(double l) {
????????????? this.l = l;
?????? }
?????? public double getH() {
????????????? return h;
?????? }
?????? public void setH(double h) {
????????????? this.h = h;
?????? }
?????? public double getArea() {
????????????? return Area;
?????? }
?????? public void setArea(double area) {
????????????? Area = area;
?????? }
?????? public double getPerimeter() {
????????????? return Perimeter;
?????? }
?????? public void setPerimeter(double perimeter) {
????????????? Perimeter = perimeter;
?????? }
?????? public void paintBorder(){
????????????? flag1=true;
????????????? System.out.println("正在為長(zhǎng)方形的邊著色~~");
?????? }
?????? public void paintShape(){
????????????? flag2=true;
????????????? System.out.println("正在為長(zhǎng)方形的內(nèi)部著色~~");
?????? }
?????? public boolean isShapeBorder() {
??? if (flag1)
??????? return true;
??? else
??????? return false;
}
?????? public boolean isShapePainted() {
??? if (flag2)
??????? return true;
??? else
??????? return false;
}
?????? public void perimeter() {
????????????? double ll =
2*getH()+2*getL();
????????????? System.out.println("長(zhǎng)方形周長(zhǎng)為:"+ll);
??? setPerimeter(ll);
?????? }
?????? public void area() {
????????????? System.out.println("長(zhǎng)方形面積為:"+getH()*getL());
??? setArea(getH()*getL());
?????? }
}
Shape2D.java
package shiyan4_2;
public abstract class Shape2D {
?????? public abstract void perimeter();
?????? public abstract void area();
?????? public abstract boolean isShapeBorder() ;
?????? public abstract boolean isShapePainted();
?????? public abstract double getArea() ;
?????? public abstract double getPerimeter();
}
SoliderColorable.java
package shiyan4_2;
public interface SoliderColorable {
public void paintShape();
public boolean isShapePainted();
}
Square.java
package shiyan4_2;
public class Square extendsRectangle {
??? private double a = 0;
? ??? private boolean flag1 = false;
? ??? private boolean flag2 = false;
? ??? private double Area;
? ??? private double Perimeter;
??? public double getArea() {
???????????????????? return Area;
????????????? }
????????????? public void setArea(double area) {
???????????????????? Area = area;
????????????? }
????????????? public double getPerimeter() {
???????????????????? return Perimeter;
????????????? }
????????????? public void setPerimeter(double perimeter) {
???????????????????? Perimeter = perimeter;
????????????? }
????????????? publicSquare(double width) {
??????? super(width,width);
??????? if (width>0) {
??????????? this.a = width;
??????????? System.out.println("構(gòu)成了一個(gè)邊長(zhǎng)為:"+this.a+"的正方形");
??????????? setSuccess(true);
??????? }else {
??????????? setSuccess(false);
??????? }
??? }
????????????? private void setSuccess(boolean b) {
?? ?? ?if(b){
?? ?? ?}else{
?? ????????? ?System.out.println("創(chuàng)建失敗,輸入有問題单匣。");
?? ?? ?}
????????????? }
??? public double getA() {
??????? return a;
??? }
??? public void setA(double a) {
??????? this.a = a;
??? }
??? public void area() {
??????? this.setArea(getA()
* getA());
??????? System.out.println("正方形面積為:" +
getA() * getA());
??? }
??? public void perimeter() {
????? ??this.setPerimeter(getA() * 4);
??????? System.out.println("正方形周長(zhǎng)為:" +
getA() * 4);
??? }
? ??? public void paintBorder(){
? ?????????? flag1=true;
? ?????????? System.out.println("正在為正方形的邊著色~~");
? ??? }
? ??? public void paintShape(){
? ?????????? flag2=true;
? ?????????? System.out.println("正在為正方形的內(nèi)部著色~~");
? ??? }
? ??? public boolean isShapeBorder() {
????? if (flag1)
????????? return true;
????? else
????????? return false;
? }
? ??? public boolean isShapePainted() {
????? if (flag2)
????????? return true;
????? else
????????? return false;
? }
}
Test.java
package shiyan4_2;
import java.util.Scanner;
public class Test {
??? public static void main(String[] agrs){
??????? Scannerinput=new Scanner(System.in);
??????? CostCalculatorcost=new CostCalculator(1.0,2.0);//邊的花費(fèi),內(nèi)部的花費(fèi)
??????? System.out.println("著色一米的邊長(zhǎng)需要:"+cost.getBorderCost()+"RMB??????"+"著色l平方米需要:"+cost.getSoliderCost()+"RMB");
??????? double radius=0;
??????? double height=0,width=0;
??????? double left=0,right=0,boom=0;
??????? //*******圓*******
??????? System.out.print("請(qǐng)輸入圓的半徑:");
??????? radius=input();
??????? Circlecircle=new Circle(radius);
??????? circle.area();
??????? circle.perimeter();
??????? circle.paintBorder();//為邊著色
??????? System.out.println("花費(fèi)為:"+cost.calculate(circle));
??????? //*******長(zhǎng)方形*******
??? ????System.out.print("請(qǐng)輸入長(zhǎng)方形的長(zhǎng):");
??????? height=input();
??????? System.out.print("請(qǐng)輸入長(zhǎng)方形的寬:");
??????? width=input();
??????? Rectanglerectangle=new Rectangle(height,width);???????
??????? rectangle.area();
??????? rectangle.perimeter();
??????? rectangle.paintBorder();//為邊著色
??????? rectangle.paintShape();//為內(nèi)部著色
??????? System.out.println("花費(fèi)為:"+cost.calculate(rectangle));???
??????? //******正方形******
??????? System.out.print("請(qǐng)輸入正方形的邊長(zhǎng):");
??????? width=input();
??????? Squaresquare=new Square(width);
??????? square.area();
??????? square.perimeter();
??????? square.paintBorder();//為內(nèi)部著色
??????? square.paintShape();
??????? System.out.println("花費(fèi)為:"+cost.calculate(square));
??????? //******三角形******
??????? do{
???????? ???System.out.print("請(qǐng)輸入三角形的第一邊長(zhǎng):");
??????????? left=input();
??????????? System.out.print("請(qǐng)輸入三角形的第二邊長(zhǎng):");
??????????? right=input();
??????????? System.out.print("請(qǐng)輸入三角形的第三邊長(zhǎng):");
??????????? boom=input();
??????? }while(!judge(left,right,boom));???
?? ?????Triangletriangle=new Triangle(left,right,boom);;
??????? triangle.area();
??????? triangle.perimeter();
??????? triangle.paintBorder();//為邊著色
??????? triangle.paintShape();//為內(nèi)部著色???????
??????? System.out.println("花費(fèi)為:"+cost.calculate(triangle));
??????? input.close();
??? }
??? public static boolean judgement(double r){
??????? if(r<0){
??????????? System.out.println("輸入錯(cuò)誤夕凝,請(qǐng)重新輸入");
??????????? return false;
??????? }
??????? return true;
??? }
??? public static double input(){
??????? double temp=0;
??????? boolean flag=true;
??????? Scannerinput=new Scanner(System.in);//不能關(guān)閉否則無限循環(huán)
??????? do{
??????????? flag=true;
??????????? input=new Scanner(System.in);
??????????? if(input.hasNextDouble()){
??????????????? temp=input.nextDouble();
? ??????????????if(temp>0){
??????????????????? flag=false;
??????????????????? break;
??????????????? }
??????????? }???
??????????? System.out.println("輸入必須是double型宝穗,并且大于0,請(qǐng)重新輸入");
??????? }while(flag);
??????? return temp;
??? }
??? public static boolean judge(double right,double left,double boom){???????
??????????? if(right<=0 || left<=0 || boom<=0){
??????????????? System.out.println("輸入不滿足三角形構(gòu)造規(guī)則码秉,請(qǐng)重新輸入");
??????????????? return false;
??????????? }
??????????? if(right+left<=boom ||right+boom<=left ||boom+left<=right){
??????????????? System.out.println("輸入不滿足三角形構(gòu)造規(guī)則逮矛,請(qǐng)重新輸入");
??????????????? return false;
??????????? }
??????????? if(right-left>=boom ||right-boom>=left ||boom-left>=right){
??????????????? System.out.println("輸入不滿足三角形構(gòu)造規(guī)則,請(qǐng)重新輸入");
???????????????return false;
??????????? }
??????????? return true;
??????? }
}
Triangle.java
package shiyan4_2;
public class Triangle extendsShape2D implements SoliderColorable{
??? private double length1;
??? private double length2;
??? private double length3;
? ??? private boolean flag1 = false;
? ??? private boolean flag2 = false;
? ??? private double Area;
? ??? private double Perimeter;
???? public double getArea() {
???????????????????? return Area;
????????????? }
????????????? public void setArea(double area) {
???????????????????? Area = area;
????????????? }
????????????? public double getPerimeter() {
???????????????????? return Perimeter;
????????????? }
????????????? public void setPerimeter(double perimeter) {
???????????????????? Perimeter = perimeter;
????????????? }
????????????? publicTriangle(double length1, double length2, double length3) {
???????? // TODO Auto-generated constructor stub
???????? super();
??????? this.length1 = length1;
??????? this.length2 = length2;
???????? this.length3 = length3;
??????? if((Math.abs(this.length1 - this.length2) < this.length3)
??????????????? && (Math.abs(this.length1 - this.length3) < this.length2)
??????????????? && (Math.abs(this.length3 - this.length2) < this.length1)
???????????????? && (Math.abs(this.length1 + this.length2) > this.length3)
??????????????? && (Math.abs(this.length1 + this.length3) > this.length2)
???????????????? && (Math.abs(this.length3 + this.length2) > this.length1)) {
??????????? setSuccess(true);
?????????? System.out.println("構(gòu)成了一個(gè)三條邊長(zhǎng)分別為:" + this.length1 + ","
??????????????????? +this.length2 + "," + this.length3 + "的三角形");
???????? }else {
??????????? setSuccess(false);
??????? }
???? }
???? private void setSuccess(boolean b) {
???? ?if(b){
???? ?}else{
??? ??????? ?System.out.println("創(chuàng)建失敗转砖,輸入有問題橱鹏。");
???? ?}
????????????? }
????????????? public double getLength1() {
??????? return length1;
???? }
???? public void setLength1(double length1) {
??????? this.length1 = length1;
???? }
???? public double getLength2() {
???????? return length2;
???? }
??? public void setLength2(double length2) {
??????? this.length2 = length2;
??? }
???? public double getLength3() {
??????? return length3;
???? }
???? public void setLength3(double length3) {
???????? this.length3 = length3;
???? }
???? public void area() {
???????? double p =
(getLength1() + getLength2() + getLength3()) / 2;
???????? System.out.println("面積為:"
???????????????? + Math.sqrt(p *
(p -
getLength1()) * (p - getLength2())
???????????????????????? * (p -
getLength3())));
???????? this.setArea(Math.sqrt(p *
(p -
getLength1()) * (p - getLength2())
???????????????????????? * (p -
getLength3())));
?? ??}
???? public void perimeter() {
???????? System.out.println("周長(zhǎng)為:"
???????????????? + (getLength1() + getLength2()+ getLength3()));
???????? this.setPerimeter((getLength1()
+ getLength2() + getLength3()));
???? }
?? ?? public void paintBorder(){
? ?????????? flag1=true;
? ?????????? System.out.println("正在為三角形的邊著色~~");
? ??? }
? ??? public void paintShape(){
? ?????????? flag2=true;
? ?????????? System.out.println("正在為三角形的內(nèi)部著色~~");
? ??? }
? ??? public boolean isShapeBorder() {
????? if (flag1)
????????? return true;
????? else
????????? return false;
? }
? ??? public boolean isShapePainted() {
????? if (flag2)
????????? return true;
????? else
????????? return false;
? }
}