编程习题

三、程序设计   共4题 (共计40分)
第1题 (10.0分)        题号:930        难度:中        第3章
/*-------------------------------------------------------
【程序设计】
---------------------------------------------------------


题目:设计一个描述二维平面上点的类Point,要求如下:
(a)该类需要描述点的浮点型的横坐标x和纵坐标y。
(b)提供能够初始化横纵坐标的有参构造方法,要求参数名称与属性同名。
(c)计算两点间距离的方法distance。
提示:两点之间距离等于两点横纵坐标之差的平方和再开方
Math类中求平方根的方法:static double sqrt(double  a)




-------------------------------------------------------*/


/**********Program**********/






/**********  End  **********/


public class Prog1{
  public static void main(String[] args){
     Point p1=new Point(2,3);
     Point p2=new Point(4,5);
     System.out.println("两个点的距离:"+p1.distance(p2)); 
  }
}


答案:class Point {
   double x;
   double y;
   
   public Point(double x,double y){
  this.x=x;
  this.y=y;
   }
   
   public double distance(Point p){
  return Math.sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y));
   }
}




第2题 (10.0分)        题号:928        难度:中        第3章
/*-------------------------------------------------------
【程序设计】
---------------------------------------------------------


题目:定义一个电风扇类Fan,要求如下:
(a)属性包括:电风扇型号model(String类型)、价格price(double类型)
     和开关状态running(boolean类型),并且所有属性为私有。
(b)至少提供一个有参的构造方法(要求型号可以初始化为任意值,价格不能小于0,
     开关状态必须为false)。
(c)为私有属性提供访问器方法。


-------------------------------------------------------*/


/**********Program**********/






/**********  End  **********/


public class Prog1{
  public static void main(String[] args){
       Fan  m1=new Fan("吊扇",300); 
  }
}




答案:class Fan {
    private String model;
    private double price;
    private boolean running;
    public Fan(String model,double price){
    this.model=model;
    if(price >=0)
    this.price=price;
    this.running=false;
   
    }
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public boolean isRunning() {
return running;
}
public void setRunning(boolean running) {
this.running = running;
}
public String getModel() {
return model;
}

}




第3题 (10.0分)        题号:846        难度:中        第7章
/*-------------------------------------------------------
【程序设计】
---------------------------------------------------------


题目:现有父类Goods,在此基础上派生出子类Apple,子类定义了自己的属性String 类型的类别(kind),
有带参数的构造方法,覆盖了父类的show方法,调用父类被覆盖的show方法,增加打印自己的属性的语句,
请实现Apple类的编写。
-------------------------------------------------------*/
class Goods {
        double unitPrice;//单价
        double account;//数量
        Goods(double unitPrice, double account) {
                this.unitPrice=unitPrice ;
                this.account=account ;
        }
        double totalPrice() {//计算总价格
                return unitPrice * account;
        }
        void show() {
                System.out.println("单价是" + unitPrice);
                System.out.println("购买数量为" + account);
                System.out.println("购买商品的总金额是" + this.totalPrice());
        }
}
class Apple extends Goods {
/**********Program**********/




/**********  End  **********/
}
public class Prog1{
public static void main(String[] args){
}
}




答案:String kind;
Apple(double unitPrice, double account, String kind) {
super(unitPrice, account);
this.kind = kind;
}
double totalPrice() {
return unitPrice * account;
}
void show() {
System.out.println("苹果的类别是" + kind);
super.show();
}




第4题 (10.0分)        题号:903        难度:中        第21章
/*-------------------------------------------------------
【程序设计】
---------------------------------------------------------


题目:声明一个抽象类Car,该类有颜色color(String类型)
和汽车速度speed(double类型)两个私有属性,有带一个参数的构造
方法,能够利用参数初始化颜色Color。有两个私有属性的访问器方法。
还有一个抽象的startup方法,该方法无返回值无参。
再声明一个Car的子类Benz。该类覆盖startup方法,输出“benz startup”


-------------------------------------------------------*/


/**********Program**********/




/**********  End  **********/


public class Prog1{
  public static void main(String[] args){
       Car  c1 = new Benz("black");
  }
}


答案:abstract class Car
{
   private String color;
   private double speed;
   public Car(String color){
     this.color = color;
   }
   public void setColor(String color)
   {
       this.color = color;
   }
   public String getColor(){
      return color;
   }
   public void setSpeed(double speed){
      this.speed = speed;
   }
   public double getSpeed(){
      return speed;
   }
   abstract void startup();
}
class Benz extends Car{
   public Benz(String color){
      super(color);
   }
   public void startup(){
       System.out.println("benz startup");
   }
}
三、程序设计   共4题 (共计40分)
第1题 (10.0分)        题号:933        难度:中        第3章
/*-------------------------------------------------------
【程序设计】
---------------------------------------------------------


题目:设计一个汽车类Vehicle,要求如下:
(a)属性有车轮个数wheels(int类型)和车重weight(double类型)。
(b)构造方法包括:带参数的构造方法,能够初始化车轮个数和车重
(c)方法包括:输出所有属性的printInfo方法




-------------------------------------------------------*/


/**********Program**********/






/**********  End  **********/


public class Prog1{
  public static void main(String[] args){
    Vehicle v=new Vehicle(3,5.0);


  }
}


答案:class Vehicle {
   int wheels;
   double weight;
   public Vehicle(int wheels,double weight){
  this.wheels=wheels;
  this.weight=weight;
   }   
   public void printInfo(){
  System.out.println("车轮数:"+wheels);
  System.out.println("车载重:"+weight);
   }
}




第2题 (10.0分)        题号:932        难度:中        第3章
/*-------------------------------------------------------
【程序设计】
---------------------------------------------------------


题目:设计一个关于等边三角形的类Trival,要求如下:
(a)属性包括三角形的边side(double类型)
(b)构造方法包括:无参构造方法和为side指定初值的有参构造方法
(c)方法包括:获取三角形面积findArea()。




-------------------------------------------------------*/


/**********Program**********/






/**********  End  **********/


public class Prog1{
  public static void main(String[] args){
    Trival t=new Trival(3.0);


  }
}


答案:class Trival{
double side;
public Trival(double side){
this.side=side; 
}
public Trival(){
this.side=1;
}
public double findArea(){
return Math.sqrt(3)*side*side/4; 
}
}




第3题 (10.0分)        题号:843        难度:中        第7章
/*-------------------------------------------------------
【程序设计】
---------------------------------------------------------


题目:现有父类Person,在此基础上派生出子类Teacher,子类定义了自己的属性String类型的教师编号(teacherID),
有不带参数的构造方法,覆盖了父类的print方法,调用父类被覆盖的print方法,增加打印自己的属性的语句,
请实现Teacher类的编写。


-------------------------------------------------------*/
class Person {
        String id;
        String name;


        Person(String id, String name) {
                this.id = id;
                this.name = name;
        }


        void print() {
                System.out.println("id =" + id + ",name =" + name);
        }
}
class Teacher extends Person {
/**********Program**********/






/**********  End  **********/
}


public class Prog1{
public static void main(String[] args) {


}
}




答案:String teacherID;
Teacher() {
super("2201212", "rose");
teacherID = "10010101";
}
void print() {
super.print();
System.out.println("teacherID =" + teacherID);
}




第4题 (10.0分)        题号:905        难度:中        第21章


/*-------------------------------------------------------
【程序设计】
---------------------------------------------------------


题目:按要求将下面程序补充完整:
已有接口CanFly和CanJump,分别描述飞和跳的能力。要求:
声明Locust(蝗虫)类和Balloon(气球)类,Locust类具有飞和跳
的能力,Balloon类具有飞的能力。飞和跳的方法可以通过输出相应
的字符串来实现。
然后,在主类Prog1中,添加一个静态的方法testFly(),其功能是
让能飞的物体执行飞的动作。在main方法中分别创建Locust类和
Balloon类的对象,调用testFly()方法。




-------------------------------------------------------*/


interface CanFly {
        public void fly();


}
interface CanJump {
        public void jump();
}
/**********Program**********/






public class Prog1{


  public static void main(String[] args){
       testFly(new Locust());
       testFly(new Balloon());
  }
}
/**********  End  **********/






答案:class Locust implements CanFly,CanJump{
public void jump() {
System.out.println("locust is jumping");
}
public void fly() {
System.out.println("locust is flying");
}
}
class Balloon implements CanFly{
public void fly(){
System.out.println("balloon is flying");
}
}


public class Prog1{
static void testFly(CanFly c){
           c.fly();
        }
  public static void main(String[] args){
       testFly(new Locust());
       testFly(new Balloon());
  }
}


三、程序设计   共4题 (共计40分)
第1题 (10.0分)        题号:842        难度:中        第3章
/*-------------------------------------------------------
【程序设计】
---------------------------------------------------------


题目:定义类,属性包括商品名称name(String)、商品编号id(String)和价格price(double)三个属性,
有无参的构造方法和计算折扣价格并输出的方法,方法头为public void computeDiscout(double percent),
其中形参代表打折的百分比。
创建商品类的对象,调用计算折扣价格的方法。


-------------------------------------------------------*/
public class Prog1{
/**********Program**********/






/**********  End  **********/
}




答案:String name;
String id;
double price;
Prog1(){
name="虾条"; id="g009";
price=3.5;
}
public void computeDiscout(double percent) {
System.out.println(name + "原价为" + price+"元");
price = price * percent;
System.out.println(name + "折扣后的价格是" + price+"元");
}
public static void   main    (String[] args) {
Prog1 g=new Prog1();
g.computeDiscout(0.9);
}




第2题 (10.0分)        题号:928        难度:中        第3章
/*-------------------------------------------------------
【程序设计】
---------------------------------------------------------


题目:定义一个电风扇类Fan,要求如下:
(a)属性包括:电风扇型号model(String类型)、价格price(double类型)
     和开关状态running(boolean类型),并且所有属性为私有。
(b)至少提供一个有参的构造方法(要求型号可以初始化为任意值,价格不能小于0,
     开关状态必须为false)。
(c)为私有属性提供访问器方法。


-------------------------------------------------------*/


/**********Program**********/






/**********  End  **********/


public class Prog1{
  public static void main(String[] args){
       Fan  m1=new Fan("吊扇",300); 
  }
}




答案:class Fan {
    private String model;
    private double price;
    private boolean running;
    public Fan(String model,double price){
    this.model=model;
    if(price >=0)
    this.price=price;
    this.running=false;
   
    }
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public boolean isRunning() {
return running;
}
public void setRunning(boolean running) {
this.running = running;
}
public String getModel() {
return model;
}

}




第3题 (10.0分)        题号:845        难度:中        第7章
/*-------------------------------------------------------
【程序设计】
---------------------------------------------------------


题目:现有父类Goods,在此基础上派生出子类Milk,子类定义了自己的属性double类型的会员价格(vipPrice),
有带参数的构造方法,覆盖了父类的show方法,调用父类被覆盖的show方法,增加打印自己的属性的语句,
请实现Milk类的编写


-------------------------------------------------------*/
class Goods {
        double unitPrice;//单价
        double account;//数量
        Goods(double unitPrice, double account) {
                this.unitPrice=unitPrice ;
                this.account=account ;
        }
        double totalPrice() {//计算总价格
                return unitPrice * account;
        }
        void show() {
                System.out.println("单价是" + unitPrice);
                System.out.println("购买数量为" + account);
                System.out.println("购买商品的总金额是" + this.totalPrice());
        }
}
class Milk extends Goods {
/**********Program**********/




/**********  End  **********/
}
public class Prog1{
public static void main(String[] args){
}
}




答案:double vipPrice;
Milk(double n, double p, double vipPrice) {
super(n, p);
this.vipPrice = vipPrice;
}
void show() {
super.show();
System.out.println("会员价格是" + vipPrice);
}




第4题 (10.0分)        题号:904        难度:中        第21章
/*-------------------------------------------------------
【程序设计】
---------------------------------------------------------


题目:声明一个抽象类User,该类有用户名name(String类型)
和密码pass(String类型)两个私有属性,有带参数的构造方法,
能够利用参数初始化name和pass。有两个私有属性的访问器方法。
还有一个抽象的login方法,该方法无返回值无参。
再声明一个User的子类Admin。该类覆盖login方法,输出“admin login”


-------------------------------------------------------*/


/**********Program**********/




/**********  End  **********/


public class Prog1{
  public static void main(String[] args){
       User  u1 = new Admin("admin","11");
  }
}




答案:abstract class User
{
   private String name;
   private String pass;
   public User(String name,String pass){
     this.name = name;
     this.pass = pass;
   }
   public void setName(String name)
   {
       this.color = color;
   }
   public String getName(){
      return name;
   }
   public void setPass(String pass){
      this.pass = pass;
   }
   public double getPass(){
      return pass;
   }
   abstract void login();
}
class Admin extends User{
   public Admin(String name,String pass){
      super(name,pass);
   }
   public void login(){
       System.out.println("admin login");
   }
}


三、程序设计   共4题 (共计40分)
第1题 (10.0分)        题号:839        难度:中        第3章
/*-------------------------------------------------------
【程序设计】
---------------------------------------------------------


题目:定义一个名为Prog1的类,属性有平时成绩(pingshi),期末成绩(qimo),都为int类型;
不带参数的构造方法,方法有计算并输出总成绩的方法calculateScore(),
计算方式为:总成绩=平时成绩+期末成绩的1/2;在main方法中,创建Prog1对象s,
然后调用calculateScore()方法来输出总成绩。


-------------------------------------------------------*/
public class Prog1{
/**********Program**********/






/**********  End  **********/
}




答案:int pingshi;
int qimo;
Prog1() {
pingshi =45;
qimo = 56;
}
void calculateScore() {
double score = pingshi + qimo * 0.5;
System.out.print("分数为:" + score);
}
public static void main(String[] args) {
Prog1 s = new Prog1();
s.calculateScore();
}






第2题 (10.0分)        题号:927        难度:中        第3章
/*-------------------------------------------------------
【程序设计】
---------------------------------------------------------


题目:定义一个MP3类,要求如下:
(a)属性包括:MP3品牌brand(String类型)、颜色color(String类型)和存储容量capacity(double类型),
    并且所有属性为私有。
(b)至少提供一个有参的构造方法(要求品牌和颜色可以初始化为任意值,但存储容量的初始值必须为0)。
(c)为私有属性提供访问器方法。


-------------------------------------------------------*/


/**********Program**********/






/**********  End  **********/


public class Prog1{
  public static void main(String[] args){
       MP3  m1=new MP3("ipod","white"); 
  }
}


答案:class MP3 {
private String brand;
private String color;
private double capacity;
public MP3(String brand,String color){
this.brand=brand;
this.color=color;
capacity=0;

}
public double getCapacity() {
return capacity;
}
public void setCapacity(double capacity) {
this.capacity = capacity;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getBrand() {
return brand;
}

}




第3题 (10.0分)        题号:846        难度:中        第7章
/*-------------------------------------------------------
【程序设计】
---------------------------------------------------------


题目:现有父类Goods,在此基础上派生出子类Apple,子类定义了自己的属性String 类型的类别(kind),
有带参数的构造方法,覆盖了父类的show方法,调用父类被覆盖的show方法,增加打印自己的属性的语句,
请实现Apple类的编写。
-------------------------------------------------------*/
class Goods {
        double unitPrice;//单价
        double account;//数量
        Goods(double unitPrice, double account) {
                this.unitPrice=unitPrice ;
                this.account=account ;
        }
        double totalPrice() {//计算总价格
                return unitPrice * account;
        }
        void show() {
                System.out.println("单价是" + unitPrice);
                System.out.println("购买数量为" + account);
                System.out.println("购买商品的总金额是" + this.totalPrice());
        }
}
class Apple extends Goods {
/**********Program**********/
  String kind;
  public Apple(double unitPrice,double account,String kind){
     super(unitPrice,account);
     this.kind=kind;
  }
  void show(){
super.show();
System.out.println(kind);
  }


/**********  End  **********/
}
public class Prog1{
public static void main(String[] args){
}
}




答案:String kind;
Apple(double unitPrice, double account, String kind) {
super(unitPrice, account);
this.kind = kind;
}
double totalPrice() {
return unitPrice * account;
}
void show() {
System.out.println("苹果的类别是" + kind);
super.show();
}




第4题 (10.0分)        题号:903        难度:中        第21章
/*-------------------------------------------------------
【程序设计】
---------------------------------------------------------


题目:声明一个抽象类Car,该类有颜色color(String类型)
和汽车速度speed(double类型)两个私有属性,有带一个参数的构造
方法,能够利用参数初始化颜色Color。有两个私有属性的访问器方法。
还有一个抽象的startup方法,该方法无返回值无参。
再声明一个Car的子类Benz。该类覆盖startup方法,输出“benz startup”


-------------------------------------------------------*/


/**********Program**********/




/**********  End  **********/


public class Prog1{
  public static void main(String[] args){
       Car  c1 = new Benz("black");
  }
}


答案:abstract class Car
{
   private String color;
   private double speed;
   public Car(String color){
     this.color = color;
   }
   public void setColor(String color)
   {
       this.color = color;
   }
   public String getColor(){
      return color;
   }
   public void setSpeed(double speed){
      this.speed = speed;
   }
   public double getSpeed(){
      return speed;
   }
   abstract void startup();
}
class Benz extends Car{
   public Benz(String color){
      super(color);
   }
   public void startup(){
       System.out.println("benz startup");
   }
}




三、程序设计   共4题 (共计40分)
第1题 (10.0分)        题号:841        难度:中        第3章
/*-------------------------------------------------------
【程序设计】
---------------------------------------------------------


题目:定义一个名为Prog1的类,属性包括name(书名,String类型)、author(作者名,String类型)、
price(书的价格,double类型),定义不带参数的构造方法,
定义输出图书基本信息的show方法。在main方法中,创建Prog1类的一个对象b,调用show方法。


-------------------------------------------------------*/
public class Prog1{
/**********Program**********/






/**********  End  **********/
}




答案:String name;
String author;
double price;
Prog1(){
name="文化苦旅";
author="余秋雨";
price=56;
}
void show(){
System.out.println("书名是"+name);
System.out.println("作者是"+author);
System.out.println("价格是"+price);
}  
public static void main(String[] args) {
Prog1 b  =  new Prog1();
b.show();
}




第2题 (10.0分)        题号:931        难度:中        第3章
/*-------------------------------------------------------
【程序设计】
---------------------------------------------------------


题目:设计一个模拟银行账户功能的类Account,要求如下:
(a)属性:账号(card,字符串类型)、储户姓名(name,字符串类型)、
    地址(address,字符串类型)、存款余额(balance,浮点型)、
    最低余额(minBalance,浮点型)
(b)方法:初始化所有属性的构造方法、存款(deposit方法)、取款(draw方法)
   、查询(query方法)
要求:存款操作需显示储户原有余额、将要存款数额及最终存款余额;
取款时,若最后余额小于最小余额,拒绝取款并显示"取款失败,至少保留余额XXX";
查询操作能显示储户账号、姓名、地址、存款余额及最低余额。


-------------------------------------------------------*/


/**********Program**********/






/**********  End  **********/


public class Prog1{
  public static void main(String[] args){
    Account a=new Account("001","John","大连",1000);
    Account.minBalance=200;
    a.deposit(500);
    a.draw(2000);
    a.query();


  }
}


答案:public class Account {
  String card;
  String name;
  String address;
  double balance;
  static double minBalance;
  
  public Account(String card,String name,String address,double balance){
 this.card=card;
 this.name=name;
 this.address=address;
 this.balance=balance;
  }
  
  public void deposit(double n){
 System.out.println("原有余额:"+balance);
 System.out.println("将要存款额:"+n);
 this.balance=this.balance+n;
 System.out.println("现有余额:"+balance);
  }
  public void draw(double m){

 if(balance-m<minBalance){
System.out.println("取款失败,至少保留余额"+minBalance); 
 }
 else  balance=balance-m;
  }
  public void query(){
 System.out.println("卡号:"+card);
 System.out.println("姓名:"+name);
 System.out.println("地址:"+address);
 System.out.println("余额:"+balance);
 System.out.println("最低余额:"+minBalance);
  }
  
}




第3题 (10.0分)        题号:845        难度:中        第7章
/*-------------------------------------------------------
【程序设计】
---------------------------------------------------------


题目:现有父类Goods,在此基础上派生出子类Milk,子类定义了自己的属性double类型的会员价格(vipPrice),
有带参数的构造方法,覆盖了父类的show方法,调用父类被覆盖的show方法,增加打印自己的属性的语句,
请实现Milk类的编写


-------------------------------------------------------*/
class Goods {
        double unitPrice;//单价
        double account;//数量
        Goods(double unitPrice, double account) {
                this.unitPrice=unitPrice ;
                this.account=account ;
        }
        double totalPrice() {//计算总价格
                return unitPrice * account;
        }
        void show() {
                System.out.println("单价是" + unitPrice);
                System.out.println("购买数量为" + account);
                System.out.println("购买商品的总金额是" + this.totalPrice());
        }
}
class Milk extends Goods {
/**********Program**********/




/**********  End  **********/
}
public class Prog1{
public static void main(String[] args){
}
}




答案:double vipPrice;
Milk(double n, double p, double vipPrice) {
super(n, p);
this.vipPrice = vipPrice;
}
void show() {
super.show();
System.out.println("会员价格是" + vipPrice);
}




第4题 (10.0分)        题号:903        难度:中        第21章
/*-------------------------------------------------------
【程序设计】
---------------------------------------------------------


题目:声明一个抽象类Car,该类有颜色color(String类型)
和汽车速度speed(double类型)两个私有属性,有带一个参数的构造
方法,能够利用参数初始化颜色Color。有两个私有属性的访问器方法。
还有一个抽象的startup方法,该方法无返回值无参。
再声明一个Car的子类Benz。该类覆盖startup方法,输出“benz startup”


-------------------------------------------------------*/


/**********Program**********/




/**********  End  **********/


public class Prog1{
  public static void main(String[] args){
       Car  c1 = new Benz("black");
  }
}


答案:abstract class Car
{
   private String color;
   private double speed;
   public Car(String color){
     this.color = color;
   }
   public void setColor(String color)
   {
       this.color = color;
   }
   public String getColor(){
      return color;
   }
   public void setSpeed(double speed){
      this.speed = speed;
   }
   public double getSpeed(){
      return speed;
   }
   abstract void startup();
}
class Benz extends Car{
   public Benz(String color){
      super(color);
   }
   public void startup(){
       System.out.println("benz startup");
   }
三、程序设计   共4题 (共计40分)
第1题 (10.0分)        题号:927        难度:中        第3章
/*-------------------------------------------------------
【程序设计】
---------------------------------------------------------


题目:定义一个MP3类,要求如下:
(a)属性包括:MP3品牌brand(String类型)、颜色color(String类型)和存储容量capacity(double类型),
    并且所有属性为私有。
(b)至少提供一个有参的构造方法(要求品牌和颜色可以初始化为任意值,但存储容量的初始值必须为0)。
(c)为私有属性提供访问器方法。


-------------------------------------------------------*/


/**********Program**********/






/**********  End  **********/


public class Prog1{
  public static void main(String[] args){
       MP3  m1=new MP3("ipod","white"); 
  }
}


答案:class MP3 {
private String brand;
private String color;
private double capacity;
public MP3(String brand,String color){
this.brand=brand;
this.color=color;
capacity=0;

}
public double getCapacity() {
return capacity;
}
public void setCapacity(double capacity) {
this.capacity = capacity;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getBrand() {
return brand;
}

}




第2题 (10.0分)        题号:846        难度:中        第7章
/*-------------------------------------------------------
【程序设计】
---------------------------------------------------------


题目:现有父类Goods,在此基础上派生出子类Apple,子类定义了自己的属性String 类型的类别(kind),
有带参数的构造方法,覆盖了父类的show方法,调用父类被覆盖的show方法,增加打印自己的属性的语句,
请实现Apple类的编写。
-------------------------------------------------------*/
class Goods {
        double unitPrice;//单价
        double account;//数量
        Goods(double unitPrice, double account) {
                this.unitPrice=unitPrice ;
                this.account=account ;
        }
        double totalPrice() {//计算总价格
                return unitPrice * account;
        }
        void show() {
                System.out.println("单价是" + unitPrice);
                System.out.println("购买数量为" + account);
                System.out.println("购买商品的总金额是" + this.totalPrice());
        }
}
class Apple extends Goods {
/**********Program**********/




/**********  End  **********/
}
public class Prog1{
public static void main(String[] args){
}
}




答案:String kind;
Apple(double unitPrice, double account, String kind) {
super(unitPrice, account);
this.kind = kind;
}
double totalPrice() {
return unitPrice * account;
}
void show() {
System.out.println("苹果的类别是" + kind);
super.show();
}




第3题 (10.0分)        题号:850        难度:中        第12章
/*-------------------------------------------------------
【程序设计】
---------------------------------------------------------


题目:已知一个int类型的数组a,数组元素分别是{34,56,78,89,90,100},
编写程序取出数组元素的十位数(使用for循环和除法运算)并输出


-------------------------------------------------------*/
public class Prog1{
/**********Program**********/




/**********  End  **********/
}




答案:public static void main(String[] args) {
int [] a=   {34,56,78,89,90,100}   ;
for(int i=0;   i<a.length;   i++)
System.out.print(a[i]/10%10+" ");

}




第4题 (10.0分)        题号:936        难度:中        第14章
/*-------------------------------------------------------
【程序设计】
---------------------------------------------------------


题目:(Map HashMap set)已知某学校的运动场地如下:
场地类型        场地名称
羽毛球         羽毛球场地1 ,  羽毛球场地2 ,羽毛球场地3,羽毛球场地4,羽毛球场地5
乒乓球        乒乓球场地1 ,  乒乓球场地2 , 乒乓球场地3, 乒乓球场地4
篮球        篮球场地1 ,  篮球场地2 ,  篮球场地3 




请完成下列要求:
1)使用一个Map<String, String[]> map,存放上表中的场地。
2)编写newStadiumfieldList()方法初始化map,并作为返回值返回。
3)编写getStadiumfield(Map<String, String[]> map)输出map的大小,并遍历map,把键和值都输出。
4)在main方法中调用两个方法,运行结果如下所示:
场地类型为 3 种
篮  球 : 场地数量为 3个,分别为   篮球场地1  篮球场地2  篮球场地3
乒乓球 : 场地数量为 4个,分别为   乒乓球场地1  乒乓球场地2  乒乓球场地3  乒乓球场地4
羽毛球 : 场地数量为 5个,分别为   羽毛球场地1  羽毛球场地2  羽毛球场地3  羽毛球场地4  羽毛球场地5


-------------------------------------------------------*/
import java.util.*;
public class Prog1{
/**********Program**********/










/**********  End  **********/
}


答案:public static void main(String[] args) {
Map<String, String[]> map= newStadiumfield();
getStadiumfield(map);
}
public static Map<String, String[]> newStadiumfield() {


Map<String, String[]> map = new HashMap<String, String[]>();
String[] s1 = { "羽毛球场地1", "羽毛球场地2", "羽毛球场地3",  "羽毛球场地4", "羽毛球场地5"};
String[] s2 = { "乒乓球场地1", "乒乓球场地2", "乒乓球场地3", "乒乓球场地4" };
String[] s3 = { "篮球场地1", "篮球场地2", "篮球场地3" };
map.put("羽毛球", s1);
map.put("乒乓球", s2);
map.put("篮  球", s3);
return map;
}
public static void getStadiumfield(Map<String, String[]> map) {
System.out.println("场地类型为 "+map.size()+" 种");
Set <String> set = map.keySet();
for (String key : set) {
System.out.print(key + " : " );
String [] s=map.get(key);
System.out.println("场地数量为 "+s.length+"个 ");
for(String value:s){
System.out.print("\t"+value  );
}
System.out.println();
}
}


三、程序设计   共4题 (共计40分)
第1题 (10.0分)        题号:841        难度:中        第3章
/*-------------------------------------------------------
【程序设计】
---------------------------------------------------------


题目:定义一个名为Prog1的类,属性包括name(书名,String类型)、author(作者名,String类型)、
price(书的价格,double类型),定义不带参数的构造方法,
定义输出图书基本信息的show方法。在main方法中,创建Prog1类的一个对象b,调用show方法。


-------------------------------------------------------*/
public class Prog1{
/**********Program**********/






/**********  End  **********/
}




答案:String name;
String author;
double price;
Prog1(){
name="文化苦旅";
author="余秋雨";
price=56;
}
void show(){
System.out.println("书名是"+name);
System.out.println("作者是"+author);
System.out.println("价格是"+price);
}  
public static void main(String[] args) {
Prog1 b  =  new Prog1();
b.show();
}




第2题 (10.0分)        题号:847        难度:中        第7章
/*-------------------------------------------------------
【程序设计】
---------------------------------------------------------


题目:现有父类Goods,在此基础上派生出子类Clothing,子类定义了自己的属性String类型的类别(style),
有带参数的构造方法,覆盖了父类的show方法,调用父类被覆盖的show方法,增加打印自己的属性的语句,
请实现Clothing类的编写


-------------------------------------------------------*/
class Goods {
        double unitPrice;//单价
        double account;//数量
        Goods(double unitPrice, double account) {
                this.unitPrice=unitPrice ;
                this.account=account ;
        }
        double totalPrice() {//计算总价格
                return unitPrice * account;
        }
        void show() {
                System.out.println("单价是" + unitPrice);
                System.out.println("购买数量为" + account);
                System.out.println("购买商品的总金额是" + this.totalPrice());
        }
}
class Clothing extends Goods {
/**********Program**********/




/**********  End  **********/
}
public class Prog1{
public static void main(String[] args){
}
}




答案:String style;
Clothing(double unitPrice, double account, String style) {
super(unitPrice, account);
this.style = style;
}
void show() {
System.out.println("服装的类别是" + style);
super.show();
}




第3题 (10.0分)        题号:849        难度:中        第12章
/*-------------------------------------------------------
【程序设计】
---------------------------------------------------------


题目:已知一个int类型的数组a,数组元素分别是{12,11,78,34},编写程序利用for循环将数组逆序输出。


-------------------------------------------------------*/
public class Prog1{
/**********Program**********/




/**********  End  **********/
}




答案:public static void main(String[] args) {
int [] a={ 12,11,78,34};
System.out.println("逆序输入为:");
for(int i=a.length -1;i>=0;i--)
System.out.print(a[i]+" ");
}




第4题 (10.0分)        题号:938        难度:中        第14章
/*-------------------------------------------------------
【程序设计】
---------------------------------------------------------
(ArrayList)按要求完成题目编写:
1)创建ArrayList对象list,添加三个字符串对象s1、s2、s3,字符串的值分别为001、002、003
2) 通过for循环对list进行遍历,输出全部字符串
3) 查找是否包含s1,如果存在,删除
4)创建迭代器it ,使用迭代器对list进行遍历,输出全部字符串


-------------------------------------------------------*/
import java.util.*;
public class Prog1{
/**********Program**********/










/**********  End  **********/
}


答案:public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
String s1="001";
String s2="002";
String s3="003";
list.add(s1);
list.add(s2);
list.add(s3);
for (String s : list) {
System.out.println(s);
}
if (list.contains(s1)) {
list.remove(s1);
}
Iterator<String> it = list.iterator();
while (it.hasNext()) {
String s = it.next();


System.out.println(s);
}
}


三、程序设计   共4题 (共计40分)
第1题 (10.0分)        题号:929        难度:中        第3章
/*-------------------------------------------------------
【程序设计】
---------------------------------------------------------


题目:定义一个教师类Teacher,要求如下:
(a)教师的姓名(name,String类型),所授课程(course,String类型),课时数量(courseNum,int类型)和课时系数(radio,double类型),所有教师的课时系数相同。属性均是私有的。
(b)提供一个有参的构造方法(要求包括姓名,所授课程,课时数量)。
(c)为私有属性提供访问器方法。
(d)定义一个计算课时当量的courseCompute方法(课时当量=课时量*系数),返回值类型为double。
(e)定义一个计算课时费的moneyCompute()方法(课时费=课时当量*钱数,如果当量超过100,每课时30元,当量不超过100每课时20元)
(f)要求所有方法都是共有的
-------------------------------------------------------*/


/**********Program**********/






/**********  End  **********/


public class Prog1{
  public static void main(String[] args){
      Teacher.setRadio(1.2);                
      Teacher t1=new Teacher("张三","Java",64);
      System.out.println(t1.getName()+"  "+t1.getCourse()+"  "+t1.getCourseNum());
      double num=t1.courseCompute();
      System.out.println("课时当量:"+num);
      System.out.println("课时费:"+t1.moneyCompute());
 
  }
}


答案:class Teacher {
private String name;
private String course;
private double courseNum;
private static double radio;
public Teacher(String name,String course,double courseNum){
this.name=name;
this.course=course;
this.courseNum=courseNum;                                              ;
}

public void setName(String name) {
this.name = name;
}


public void setCourse(String course) {
this.course = course;
}


public void setCourseNum(double courseNum) {
this.courseNum = courseNum;
}


public static void setRadio(double radio) {
Teacher.radio = radio;
}


public String getName(){
return name;
}
public String getCourse(){
return course;
}
public double getCourseNum(){
return courseNum;
}
public static double getRadio(){
return radio;
}

public double courseCompute(){
return courseNum*radio;
}
public double moneyCompute(){
if(this.courseCompute()>100){
return (courseCompute()-100)*30+100*20;
}
else{
return this.courseCompute()*20;
}
}
}




第2题 (10.0分)        题号:843        难度:中        第7章
/*-------------------------------------------------------
【程序设计】
---------------------------------------------------------


题目:现有父类Person,在此基础上派生出子类Teacher,子类定义了自己的属性String类型的教师编号(teacherID),
有不带参数的构造方法,覆盖了父类的print方法,调用父类被覆盖的print方法,增加打印自己的属性的语句,
请实现Teacher类的编写。


-------------------------------------------------------*/
class Person {
        String id;
        String name;


        Person(String id, String name) {
                this.id = id;
                this.name = name;
        }


        void print() {
                System.out.println("id =" + id + ",name =" + name);
        }
}
class Teacher extends Person {
/**********Program**********/






/**********  End  **********/
}


public class Prog1{
public static void main(String[] args) {


}
}




答案:String teacherID;
Teacher() {
super("2201212", "rose");
teacherID = "10010101";
}
void print() {
super.print();
System.out.println("teacherID =" + teacherID);
}




第3题 (10.0分)        题号:852        难度:中        第12章
/*-------------------------------------------------------
【程序设计】
---------------------------------------------------------


题目:某一歌手参加歌曲大奖赛,有30个评委对她的进行打分,存放在一个数组里score[],分数为1到100之间的随机数,
编程利用for循环实现输出这位选手的最高分、最低分和最终得分(最终得分=总分数/评委总人数)


-------------------------------------------------------*/
public class Prog1{
/**********Program**********/




/**********  End  **********/
}




答案:public static void main(String[] args) {
int [] score  =   new int[30];
int max=0,min=100,sum=0;
double avg=0;
for(int i=0;i<score.length;i++){
score[i]=(int)(Math.random()*100+1);
sum=sum+score[i];
if(max<score[i])   max=score[i];
if(min>score[i]) min=score[i];
}
avg=sum/score.length;
System.out.println("max= "+max);
System.out.println("min= "+min);
System.out.println("avg= "+avg);
}




第4题 (10.0分)        题号:935        难度:中        第14章
/*-------------------------------------------------------
【程序设计】
---------------------------------------------------------
先把信息("0"、 "1"、 "2"、 "3"、 "4")5个字符串存储到ArrayList对象v中,然后通过迭代器i对这个对象进行遍历。




-------------------------------------------------------*/
import java.util.Vector;
import java.util.Iterator;
public class Prog1{
/**********Program**********/










/**********  End  **********/
}




答案:public static void main(String[] args) {
// 定义一个动态数组
ArrayList  v= new ArrayList();
// 通过循环向数组添加5个元素
for(int i=0;i<5;i++)
{
v.add(   String.valueOf(i)   );
}

// 把数组对象转换成迭代器对象
Iterator i = v.iterator();
// 通过迭代器对象对数组r元素进行遍历
while(i.hasNext())
{
String s = (String)i.next();
System.out.println(s);
}
}


三、程序设计   共4题 (共计40分)
第1题 (10.0分)        题号:839        难度:中        第3章
/*-------------------------------------------------------
【程序设计】
---------------------------------------------------------


题目:定义一个名为Prog1的类,属性有平时成绩(pingshi),期末成绩(qimo),都为int类型;
不带参数的构造方法,方法有计算并输出总成绩的方法calculateScore(),
计算方式为:总成绩=平时成绩+期末成绩的1/2;在main方法中,创建Prog1对象s,
然后调用calculateScore()方法来输出总成绩。


-------------------------------------------------------*/
public class Prog1{
/**********Program**********/






/**********  End  **********/
}




答案:int pingshi;
int qimo;
Prog1() {
pingshi =45;
qimo = 56;
}
void calculateScore() {
double score = pingshi + qimo * 0.5;
System.out.print("分数为:" + score);
}
public static void main(String[] args) {
Prog1 s = new Prog1();
s.calculateScore();
}






第2题 (10.0分)        题号:846        难度:中        第7章
/*-------------------------------------------------------
【程序设计】
---------------------------------------------------------


题目:现有父类Goods,在此基础上派生出子类Apple,子类定义了自己的属性String 类型的类别(kind),
有带参数的构造方法,覆盖了父类的show方法,调用父类被覆盖的show方法,增加打印自己的属性的语句,
请实现Apple类的编写。
-------------------------------------------------------*/
class Goods {
        double unitPrice;//单价
        double account;//数量
        Goods(double unitPrice, double account) {
                this.unitPrice=unitPrice ;
                this.account=account ;
        }
        double totalPrice() {//计算总价格
                return unitPrice * account;
        }
        void show() {
                System.out.println("单价是" + unitPrice);
                System.out.println("购买数量为" + account);
                System.out.println("购买商品的总金额是" + this.totalPrice());
        }
}
class Apple extends Goods {
/**********Program**********/




/**********  End  **********/
}
public class Prog1{
public static void main(String[] args){
}
}




答案:String kind;
Apple(double unitPrice, double account, String kind) {
super(unitPrice, account);
this.kind = kind;
}
double totalPrice() {
return unitPrice * account;
}
void show() {
System.out.println("苹果的类别是" + kind);
super.show();
}




第3题 (10.0分)        题号:848        难度:中        第12章
/*-------------------------------------------------------
【程序设计】
---------------------------------------------------------


题目:编写程序模拟"挑战杯"演讲大赛,共有10个评委打分,分数为1到10之间的随机数,将10个分数存放在int类型数组score中,
编程利用for循环实现计算歌手的最后得分。


-------------------------------------------------------*/
public class Prog1{
/**********Program**********/




/**********  End  **********/
}




答案:public static void main(String[] args) {
int [] score=   new int[10];
int max=0,min=100,sum=0;
double avg=0;
for(int i=0;i<score.length;i++){
score[i]=(int)(Math.random()*10+1);
sum=sum+score[i];
if(max<score[i])   max=score[i];
if(min>score[i]) min=score[i];
}
avg=sum/score.length;
System.out.println("max= "+max);
System.out.println("min= "+min);
System.out.println("avg= "+avg);
}




第4题 (10.0分)        题号:936        难度:中        第14章
/*-------------------------------------------------------
【程序设计】
---------------------------------------------------------


题目:(Map HashMap set)已知某学校的运动场地如下:
场地类型        场地名称
羽毛球         羽毛球场地1 ,  羽毛球场地2 ,羽毛球场地3,羽毛球场地4,羽毛球场地5
乒乓球        乒乓球场地1 ,  乒乓球场地2 , 乒乓球场地3, 乒乓球场地4
篮球        篮球场地1 ,  篮球场地2 ,  篮球场地3 




请完成下列要求:
1)使用一个Map<String, String[]> map,存放上表中的场地。
2)编写newStadiumfieldList()方法初始化map,并作为返回值返回。
3)编写getStadiumfield(Map<String, String[]> map)输出map的大小,并遍历map,把键和值都输出。
4)在main方法中调用两个方法,运行结果如下所示:
场地类型为 3 种
篮  球 : 场地数量为 3个,分别为   篮球场地1  篮球场地2  篮球场地3
乒乓球 : 场地数量为 4个,分别为   乒乓球场地1  乒乓球场地2  乒乓球场地3  乒乓球场地4
羽毛球 : 场地数量为 5个,分别为   羽毛球场地1  羽毛球场地2  羽毛球场地3  羽毛球场地4  羽毛球场地5


-------------------------------------------------------*/
import java.util.*;
public class Prog1{
/**********Program**********/










/**********  End  **********/
}


答案:public static void main(String[] args) {
Map<String, String[]> map= newStadiumfield();
getStadiumfield(map);
}
public static Map<String, String[]> newStadiumfield() {


Map<String, String[]> map = new HashMap<String, String[]>();
String[] s1 = { "羽毛球场地1", "羽毛球场地2", "羽毛球场地3",  "羽毛球场地4", "羽毛球场地5"};
String[] s2 = { "乒乓球场地1", "乒乓球场地2", "乒乓球场地3", "乒乓球场地4" };
String[] s3 = { "篮球场地1", "篮球场地2", "篮球场地3" };
map.put("羽毛球", s1);
map.put("乒乓球", s2);
map.put("篮  球", s3);
return map;
}
public static void getStadiumfield(Map<String, String[]> map) {
System.out.println("场地类型为 "+map.size()+" 种");
Set <String> set = map.keySet();
for (String key : set) {
System.out.print(key + " : " );
String [] s=map.get(key);
System.out.println("场地数量为 "+s.length+"个 ");
for(String value:s){
System.out.print("\t"+value  );
}
System.out.println();
}
}




三、程序设计   共4题 (共计40分)
第1题 (10.0分)        题号:929        难度:中        第3章
/*-------------------------------------------------------
【程序设计】
---------------------------------------------------------


题目:定义一个教师类Teacher,要求如下:
(a)教师的姓名(name,String类型),所授课程(course,String类型),课时数量(courseNum,int类型)和课时系数(radio,double类型),所有教师的课时系数相同。属性均是私有的。
(b)提供一个有参的构造方法(要求包括姓名,所授课程,课时数量)。
(c)为私有属性提供访问器方法。
(d)定义一个计算课时当量的courseCompute方法(课时当量=课时量*系数),返回值类型为double。
(e)定义一个计算课时费的moneyCompute()方法(课时费=课时当量*钱数,如果当量超过100,每课时30元,当量不超过100每课时20元)
(f)要求所有方法都是共有的
-------------------------------------------------------*/


/**********Program**********/






/**********  End  **********/


public class Prog1{
  public static void main(String[] args){
      Teacher.setRadio(1.2);                
      Teacher t1=new Teacher("张三","Java",64);
      System.out.println(t1.getName()+"  "+t1.getCourse()+"  "+t1.getCourseNum());
      double num=t1.courseCompute();
      System.out.println("课时当量:"+num);
      System.out.println("课时费:"+t1.moneyCompute());
 
  }
}


答案:class Teacher {
private String name;
private String course;
private double courseNum;
private static double radio;
public Teacher(String name,String course,double courseNum){
this.name=name;
this.course=course;
this.courseNum=courseNum;                                              ;
}

public void setName(String name) {
this.name = name;
}


public void setCourse(String course) {
this.course = course;
}


public void setCourseNum(double courseNum) {
this.courseNum = courseNum;
}


public static void setRadio(double radio) {
Teacher.radio = radio;
}


public String getName(){
return name;
}
public String getCourse(){
return course;
}
public double getCourseNum(){
return courseNum;
}
public static double getRadio(){
return radio;
}

public double courseCompute(){
return courseNum*radio;
}
public double moneyCompute(){
if(this.courseCompute()>100){
return (courseCompute()-100)*30+100*20;
}
else{
return this.courseCompute()*20;
}
}
}




第2题 (10.0分)        题号:845        难度:中        第7章
/*-------------------------------------------------------
【程序设计】
---------------------------------------------------------


题目:现有父类Goods,在此基础上派生出子类Milk,子类定义了自己的属性double类型的会员价格(vipPrice),
有带参数的构造方法,覆盖了父类的show方法,调用父类被覆盖的show方法,增加打印自己的属性的语句,
请实现Milk类的编写


-------------------------------------------------------*/
class Goods {
        double unitPrice;//单价
        double account;//数量
        Goods(double unitPrice, double account) {
                this.unitPrice=unitPrice ;
                this.account=account ;
        }
        double totalPrice() {//计算总价格
                return unitPrice * account;
        }
        void show() {
                System.out.println("单价是" + unitPrice);
                System.out.println("购买数量为" + account);
                System.out.println("购买商品的总金额是" + this.totalPrice());
        }
}
class Milk extends Goods {
/**********Program**********/
答案:double vipPrice;
Milk(double n, double p, double vipPrice) {
super(n, p);
this.vipPrice = vipPrice;
}
void show() {
super.show();
System.out.println("会员价格是" + vipPrice);
}




/**********  End  **********/
}
public class Prog1{
public static void main(String[] args){
}
}








第3题 (10.0分)        题号:851        难度:中        第12章
/*-------------------------------------------------------
【程序设计】
---------------------------------------------------------


题目:定义一个长度为100的布尔型数组,数组名为fig,并用for循环语句将数组所有元素赋值为false


-------------------------------------------------------*/
public class Prog1{
/**********Program**********/




/**********  End  **********/
}




答案:public static void main(String[] args) {
boolean [] fig   =   new boolean[100];
for(int i=0;i<fig.length;i++){    
 g[i]=false;
 System.out.println("fig[ "+i+" ]= "+fig[i]);
    }
   }




第4题 (10.0分)        题号:937        难度:中        第14章
/*-------------------------------------------------------
【程序设计】
---------------------------------------------------------
(ArrayList)按要求完成题目编写:
1)创建ArrayList对象list,添加三个Integer对象s1、s2、s3,值分别为20、21、22
2) 通过for循环对list进行遍历,输出全部Integer的值
3) 查找是否包含s1,如果存在,删除
4)通过foreach循环对list进行遍历,输出Integer的值






-------------------------------------------------------*/
import java.util.*;
public class Prog1{
/**********Program**********/










/**********  End  **********/
}


答案:public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<Integer>();
Integer s1 = 20;
Integer s2 = 21;
Integer s3 = 22;
list.add(s1);
list.add(s2);
list.add(s3);
for (int i=0;i<list.size();i++) {
System.out.println(list.get(i));
}
if (list.contains(s1)) {
list.remove(s1);
}
for (Integer s : list) {
System.out.println(s);
}
}











西门子PLC编程习题是一种用于检验对PLC(可编程逻辑控制器)编程技能的练习题。这些题目可以帮助工程师熟练使用西门子PLC编程软件,了解PLC的工作原理以及处理各种输入/输出设备的能力。 在这些编程习题中,通常会提供一些场景或要求,并要求根据这些要求编写PLC程序。这些题目可能涉及到控制电机、执行特定动作、监测传感器信号等。 为了解决这些编程习题,首先需要了解西门子PLC编程软件的基本功能和操作。这包括创建PLC程序、配置输入/输出设备、设置定时器和计数器、编写逻辑控制代码等。 接下来,根据题目要求进行逻辑分析和设计。在设计PLC程序时,需要考虑输入信号的条件、输出动作的逻辑、定时和计数器的使用等。 完成设计后,可以使用西门子PLC编程软件将设计转化为PLC程序代码。这包括编写逻辑控制代码、配置输入和输出设备链接、设置定时器和计数器参数等。 完成代码编写后,需要通过PLC编程软件将程序下载到PLC设备中。然后对设备进行测试,检查PLC是否按预期工作。 如果出现问题,需要检查程序代码和设备配置,进行必要的调试和修改。 通过解决这些编程习题,可以提高西门子PLC编程的技能和能力。这对于工程师来说是非常有价值的,因为PLC在自动化控制领域广泛应用,掌握PLC编程能够帮助工程师更好地应对各种自动化控制需求。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值