按照题目要求编写的圆,圆锥和测试类的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+"张");
}
}
}