1.编写一个圆类Circle,该类拥有:...,并打印输出
package test;
public class Circle {
protected double radius;
public Circle(){
radius = 0;
}
public Circle(double r){
radius = r ;
}
public double getArea() {
return radius*radius*Math.PI;
}
public double getPerimeter() {
return 2*Math.PI*radius;
}
public void show() {
System.out.println("圆的半径 :"+radius);
System.out.println("圆的周长 :"+getPerimeter());
System.out.println("圆的面积 :"+getArea());
}
public static void main (String args[]) {
Cylinder c = new Cylinder(2,3.0);
c.show();
//c.showVolume();
}
}
2.编写一个圆柱类Cylinder,它继承于上面的Circle类。还拥有:...
package test;
public class Cylinder extends Circle {
private double hight;
public Cylinder (double r, double h) {
super(r);
this.hight = h;
}
public double getVolume() {
return super.getArea()*hight;
}
void showVolume() {
System.out.println("圆柱的体积:"+getVolume());
}
}
3.求1~100能被三整除的个数是多少?并求和。
package test;
import java.awt.List;
import java.util.ArrayList;
public class DivisibleSum {
private static ArrayList arr = new ArrayList();
private static int num=0;
public static void divisible(int x) {
for (int i = 1;i<101;i++) {
if(i%x==0) {
//a[num]=i;
arr.add(i);
num++;
}
}
}
public static void main (String args[]) {
divisible(3);
System.out.println("被3整除的个数"+num);
int sum =0;
for(int i = 0; i<arr.size();i++)
{
System.out.print(arr.get(i)+",");
int s= (int) arr.get(i);
sum += s;
}
System.out.println("1~100能被3整除的数求和:"+sum);
}
}
4.输入一个字符串,判断是否为回文?
package test;
import java.util.Scanner;
public class HuiwenTest {
//输入字符串 reverse倒置,然后比较
public static void main (String agrs[]) {
System.out.print("请输入一个字符串:");
Scanner input = new Scanner(System.in);
String str = input.next();
StringBuffer buff = new StringBuffer(str);
buff.reverse();
String newstr = new String(buff);
if(str.equals(newstr)) {
System.out.println(str+" 此字符串是回文!");
}else {
System.out.println(str+" 字符串不是回文!");
}
}
}
5.冒泡排序(找出第二大的数)
package test;
public class BubbleSort {
public static int[] bubbleSort(int arr[]) {
for(int i = 0;i<arr.length-1;i++) {//冒泡趟数
for(int j=0;j<arr.length-i-1;j++) {
int temp ;
if(arr[j]>arr[j+1]) {
temp =arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
return arr;
}
public static void main(String args[]) {
int[] a = {3,26,1,4,7,29,9,7,63};
a = bubbleSort(a);
for(int i = 0; i<a.length;i++) {
System.out.print(a[i]+",");
}
System.out.println("数组中第二大的数是:" +a[a.length-2]);
}
}
创造价值,乐在分享。
欢迎加入我的Java与Android逆向开发交流QQ群,提问学习。