按照題目要求編寫的圓,圓錐和測試類的Java程序如下
Test.java文件內容如下
class Circle{
private double r;
private String color;
public Circle(double r){
this.r=r;
}
public double area(){
return Math.PI*r*r;
}
public double perimeter(){
return Math.PI*2*r;
}
public double getR(){
return this.r;
}
public void setR(double r){
this.r=r;
}
public String getColor(){
return this.color;
}
public void setColor(String color){
this.color=color;
}
public String toString(){
return "圓的半徑為"+r+",顏色為"+color;
}
}
class Cone{
private Circle c;
private double h;
private String color;
public Cone(Circle c,double h){
this.c=c;
this.h=h;
}
public double volume(){
return 1.0/3*c.area()*h;
}
public Circle getCircle(){
return this.c;
}
public void setCircle(Circle c){
this.c=c;
}
public double getH(){
return this.h;
}
public void setH(double h){
this.h=h;
}
public String getColor(){
return this.color;
}
public void setColor(String color){
this.color=color;
}
public String toString(){
return "圓錐的底面積為"+c.area()+",高為"+h+",顏色為"+color;
}
}
public class Test{
public static void main(String[] args){
Circle circle1=new Circle(2.5);
circle1.setColor("紅色");
System.out.println(circle1.toString());
System.out.println("圓的面積為"+circle1.area());
System.out.println("圓的周長為"+circle1.perimeter());
Cone circlar1=new Cone(circle1,2.7);
circlar1.setColor("藍色");
System.out.println(circlar1.toString());
System.out.println("圓錐的體積為"+circlar1.volume());
}
}
⑵ 五道java語言描述的數據結構編程題,請求給予詳細解答
第一題:
//使用集合提供的工具方法
public static List<Integer> merge(List<Integer> a, List<Integer> b) {
//a,b not null
//全部放到一個set裡面,使得元素合並
Set<Integer> set = new HashSet<Integer>(a);
set.addAll(b);
//將set裡面的元素放到列表再轉為數組
Integer[] array = new ArrayList<Integer>(set).toArray(new Integer[1]);
//升序排序
Arrays.sort(array);
//將排序後的數組轉為list
return Arrays.asList(array);
}
//自己寫的演算法, a為升序列表,b為降序列表
public static List<Integer> merge2(List<Integer> a, List<Integer> b) {
//a,b not null
int aSize = a.size();
int bSize = b.size();
List<Integer> result = new ArrayList<Integer>();
int aIndex = 0;// 升序列表從首位開始
int bIndex = bSize - 1;// 降序列表從末尾開始
int aEl;
int bEl;
// 循環終止條件為: a 或者 b 列表遍歷完
while (aIndex < aSize && bIndex >= 0) {
aEl = a.get(aIndex);
bEl = b.get(bIndex);
if (aEl < bEl) {
result.add(aEl);
aIndex++;
} else {
result.add(bEl);
bIndex--;
}
}
// 將某個未遍歷完的列表中的元素添加到結果(包括了任意一個列表為空列表的情況)
if (aIndex < aSize) {
for (int i = aIndex; i < aSize; i++) {
result.add(a.get(i));
}
}
else if (bIndex > 0) {
for (int i = bIndex; i >= 0; i--) {
result.add(b.get(i));
}
}
return result;
}
⑶ 100分,幾道簡單的Java編程題,幫忙解決一下!
不是我牛叉
以前有過類似的練習
修改了一陪冊乎下就姿褲發了
我也同意樓下的觀點 自己動手做一下
有什麼不明白的可以參考我的這個
也許你有更好的解決方法
1.
import java.util.Scanner;
public class Test_If_Switch
{
public static void main(String[] args) {
System.out.println("輸入一個數字:");
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
System.out.println("方法1:"+methodl(x));
System.out.println("方法2:"+method2(x));
}
static int methodl(int x)
{
int result = 0;
if(x<0)
result = 2*x-1;
else if(x>=0&&x<3)
result = 3*x+5;
else if(x>=3&&x<5)
result = x+1;
else if(x>=5&&x<10)
result=5*x-3;
else if(x>=10)
result = 7*x+2;
return result;
}
static int method2(int x)
{
int result = 0;
switch(x)
{
case 0:
case 1:
case 2:result = 3*x+5;break;
case 3:
case 4:result = x+1;break;
case 5:
case 6:
case 7:
case 8:
case 9:result=5*x-3;break;
case 10:result = 7*x+2;break;
default:result = 2*x-1;break;
}
return result;
}
}
2.
import java.util.Scanner;
public class Test_Tn {
public static void main(String[] args) {
System.out.println("輸入一個數字:");
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
System.out.println("第"+x+"項的值為:"+method(x));
}
static long static method(int n) {
long Tn = 0;
if (x == 1)
Tn = 1;
else
Tn = method(x - 1) + x;
return Tn;
}
}
3.
public class test
{
public static void main (String args[])
{
int n=100;
while(n<=1000)
{
int i,j,k;
i=n/100;
j=(n-i*100)/10;
k=n%10;
if((Math.pow(i, 3)+Math.pow(j, 3)+Math.pow(k, 3))==n)
System.out.print(n+String.valueOf('\t'));
n++;
}
}
}
4.
public class Money {
public static void main(String[] args)
{
int a = 100,b = 50, c =10;
for(int i =5;i<=8;i++)
for(int j = 0;j<=6;j++)
for(int k = 0;k<30;k++)
{
if(a*i+b*j+c*k==800)
System.out.println("蘆悉100元"+i+"張"+"50元"+j+"張"+"10元"+k+"張");
}
}
}