第一题有问题:1、创建Person接口(即“人”),它有setData()和getData()方法对“人”属性name、sex和birthday赋值和获得这些属性组成的字符串信息。
问题是:你说要创建一个人(接口),然后里面有方法对人的属性进行赋值?这怎么可能呢,接口是没有成员变量(属性)的,怎么能赋值?接口里只能有常量。
第二题可以答一下:
package pillar;
public class Pillar { private Geometry buttom;
private double height;
public Pillar() {
// TODO Auto-generated constructor stub
}
public Pillar(Geometry button,double height){
thisbuttom = button;
thisheight = height;
}
public double getVolume(){
return thisbuttomgetArea()height;
}
public Geometry getButtom() {
return buttom;
}
public void setButtom(Geometry buttom) {
thisbuttom = buttom;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
thisheight = height;
}
}
------------------------------------------------类分割线---------------------------------------------------------
package pillar;
public interface Geometry { double getArea();
}
------------------------------------------------类分割线---------------------------------------------------------
package pillar;
public class Circle implements Geometry { private double r;
public Circle() {
// TODO Auto-generated constructor stub
}
public Circle(double r) {
thisr = r;
}
public double getArea() { return MathPIrr;
}
public double getR() {
return r;
}
public void setR(double r) {
thisr = r;
}
}
------------------------------------------------类分割线---------------------------------------------------------
package pillar;
public class Rectangle implements Geometry { private double width;
private double height;
public Rectangle() {
// TODO Auto-generated constructor stub
}
public Rectangle(double width, double height) {
thiswidth = width;
thisheight = height;
}
public double getArea() { return thiswidththisheight;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
thiswidth = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
thisheight = height;
}
}
------------------------------------------------类分割线---------------------------------------------------------
package pillar;
public class TestPillar {
/ @param args
/
public static void main(String[] args) {
Circle c = new Circle(5);
Rectangle r = new Rectangle(3,4);
Pillar p1 = new Pillar(c,6);
Pillar p2 = new Pillar(r,6);
Systemoutprintln("圆的体积:"+p1getVolume()+"\t矩形的体积:"+p2getVolume());
}
}
第一种:方式借助于,while循环获取,提示输入内容获取输入值,然后判断如果余数为5结束循环。
int i = 0;
do{
Systemoutprintln("请输入数据边界值:");
//获取输入数字
Scanner sc = new Scanner(Systemin);
int s = scnextInt();
i = s%10;
if(i == 5){
Systemoutprintln(s);
scclose();
}
}while( i == 5 );
引入类:
第二种:用for循环实现可以控制一定的循环次数。
public static void testFor(){
for (int i = 0; i <= 10 ; i++) {
Systemoutprintln("请输入数据边界值:");
//获取输入数字
Scanner sc = new Scanner(Systemin);
int s = scnextInt();
int b = 0;
b = s%10;
if(b == 5){
Systemoutprintln(s);
scclose();
break;
}
}
}
第三种:让用户输入一个字符串,数字按逗号分隔,然后判断其中有多少数字余数为5
//实现输入一个字符窜数字集合数字以逗号分隔
public static void testString(){
Systemoutprintln("请输入数据边界值:");
//获取输入数字
Scanner sc = new Scanner(Systemin);
String s = scnext();
if(s != null && slength() > 0){
String [] sNum = ssplit(",");
for (int i = 0; i < sNumlength ; i++) {
int sn = IntegervalueOf(sNum[i]);
int b = 0;
b = sn % 10;
if(b == 5){
Systemoutprintln(sn);
}
}
}
//关闭输入流
scclose();
}
//接口
public interface Shape{
public abstract double getPeremeter();
public abstract double getArea();
}
//矩形类
public class MyRect implements Shape{
private double width = 0; //宽
private double height = 0;//长
private double arc = 0;//对角线
public MyRect(double width, double height){
thiswidth = width;
thisheight = height;
}
@Override
public double getPeremeter(){
return (width + height) 2;
}
@Override
public double getArea(){
return width height;
}
public void show(){
arc = Mathsqrt((width width) + (height height))
Systemoutprintln("长:"+ height + " 宽:" + width + " 面积:" + getArea() + " 对角线:" + arc);
}
}
public class test{
public static void main(String [] args){
MyRect myRect = new MyRect(20, 30);
Systemoutprintln("周长:" + myRectgetPeremeter());
Systemoutprintln("面积:" + myRectgetArea());
myRectshow();
}
}
以上就是关于JAVA语言程序设计两道练习题。谢谢!全部的内容,包括:JAVA语言程序设计两道练习题。谢谢!、Java 基础编程题求解,不是很懂、帮忙做一个Java程序设计题等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)