1、编程833/*-【程序设计】-题目:使用while循环和if语句实现计算并输出1-100的偶数和,循环变量名为i,存放和的变量名为sum-*/public class Prog1/*Program*/ public static void main(String args) int i=1,sum=0; while(i=100) if(i%2=0) sum+=i; i+; System.out.println(sum); /* End */834/*-【程序设计】-题目:使用while循环和if语句实现计算并输出1-100的能被3整除的数的和,循环变量名为i,存放和的变量名为sum。-*/pu
2、blic class Prog1/*Program*/ public static void main(String args) int i=1,sum=0; while(i=100) if(i%3=0) sum+=i; i+; System.out.println(sum); /* End */835/*-【程序设计】-题目:使用for循环和if语句实现输出1到100中能被7整除或者个位数是7的数字,循环变量名为i-*/public class Prog1/*Program*/ public static void main(String args) int i; for(i=1;i=100
3、;i+) if(i%7=0 | i/100=7 | i/10/10=7 | i%10=7) System.out.println(i); /* End */836/*-【程序设计】-题目:编写一个应用程序,应用for循环计算整数n的阶乘,n的初始值为8,并将结果输出,循环变量名为i,存放阶乘的变量名为p-*/public class Prog1/*Program*/ public static void main(String agrs) int i,n=8,p=1; for(i=1;i=n;i+) p*=i; System.out.println(p); /* End */837/*-【程序
4、设计】-题目:使用while循环和if语句实现计算并输出1-100的奇数和,循环变量名为i,存放和的变量名为sum-*/public class Prog1/*Program*/ public static void main(String args) int i=1,sum=0; while(i=100) if(i%2!=0) sum+=i; i+; System.out.println(sum); /* End */838/*-【程序设计】-题目:Prog1类与对象1) 属性:两个double成员变量,width和height;2) 不带参数的构造方法:width和height的初始值分别
5、是6和8;3) 方法:计算并输出矩形的周长方法名为findPremeter ();4) 创建一个Prog1类的对象,对象名为r,调用findPremeter方法。-*/public class Prog1/*Program*/ private double width,height; public Prog1() this.width=6; this.height=8; public double findPremeter() return 2*(width+height); public static void main(String args) Prog1 r =new Prog1();
6、r.findPremeter(); /* End */839/*-【程序设计】-题目:属性有平时成绩(pingshi),期末成绩(qimo),都为int类型;不带参数的构造方法,方法有计算并输出总成绩的方法calculateScore(),计算方式为:总成绩=平时成绩+期末成绩的1/2;创建Prog1对象s,然后调用calculateScore()方法来输出总成绩。-*/public class Prog1/*Program*/ private int pingshi,qimo; public Prog1() this.pingshi=40; this.qimo=90; public doub
7、le calculateScore() return pingshi+qimo/2; public static void main(String args) Prog1 s=new Prog1(); System.out.println(s.calculateScore(); /* End */*-【程序设计】-题目:包含一个名为radius属性,类型为int,不带参数的构造方法和计算并输出面积方法findArea(面积=3.14*radius*radius),创建Prog1类的一个对象c,调用findArea方法。-*/public class Prog1/*Program*/ priva
8、te int radius; public Prog1() this.radius=4; public double findArea() return 3.14*radius*radius; public static void main(String args) Prog1 c=new Prog1(); c.findArea(); /* End */841/*-【程序设计】-题目:属性包括name(书名,String类型)、author(作者名,String类型)、price(书的价格,double类型),定义不带参数的构造方法,定义输出图书基本信息的show方法。创建Prog1类的一个对
9、象b,调用show方法。-*/public class Prog1/*Program*/ private String name,author; private double price; public Prog1() this.name=Core Java; this.author=Gary Cornell; this.price=52.8; public void show() System.out.println(The book name is +name+,author is +author+,the price is +price+.); public static void ma
10、in(String args) Prog1 b=new Prog1(); b.show(); /* End */842/*-【程序设计】-题目:定义类,属性包括商品名称name(String)、商品编号id(String)和价格price(double)三个属性,有无参的构造方法和计算折扣价格并输出的方法,方法头为public void computeDiscout(double percent),其中形参代表打折的百分比。创建商品类的对象,调用计算折扣价格的方法。-*/public class Prog1/*Program*/ private String name,id; private
11、double price; public Prog1() this.name=Shen Bao; this.id=23333333; this.price=52; public void computeDiscout(double percent) this.price*=(1-percent); public static void main(String args) Prog1 shop=new Prog1(); puteDiscout(0.2); /* End */843/*-【程序设计】-题目:现有父类Person,在此基础上派生出子类Teacher,子类定义了自己的属性String类
12、型的教师编号(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*/ String t
13、eacherID; public Teacher() this.name=Shampoo; this.teacherID=23333; public void print() super.print(); System.out.println(teacherID = + teacherID + ,name = + name); /* End */public class Prog1public static void main(String args) 844/*-【程序设计】-题目:现有父类Person,在此基础上派生出子类Student,子类定义了自己的属性String类型的学号(stud
14、entID),有不带参数的构造方法,覆盖了父类的print方法,调用父类被覆盖的print方法,增加打印自己的属性的语句,请不实现Student类的编写。-*/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 Student extends Person /*Program*/ String studentID;
15、public Student() this.name=Shampoo; this.studentID=23333; public void print() super.print(); System.out.println(studentID = + studentID + ,name = + name); /* End */public class Prog1public static void main(String args)845/*-【程序设计】-题目:现有父类Good,在此基础上派生出子类Milk,子类定义了自己的属性double类型的会员价格(vipPrice),有带参数的构造方
16、法,覆盖了父类的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
17、); System.out.println(购买数量为 + account); System.out.println(购买商品的总金额是 + this.totalPrice(); class Milk extends Goods /*Program*/ double vipPrice; public Milk(double unitPrice, double account,double vipPrice) this.unitPrice=unitPrice; this.account=account ; this.vipPrice=vipPrice; public void show() su
18、per.show(); System.out.println(会员价格为 + vipPrice); /* End */public class Prog1public static void main(String args)846/*-【程序设计】-题目:现有父类Good,在此基础上派生出子类Apple,子类定义了自己的属性String 类型的类别(kind),有带参数的构造方法,覆盖了父类的show方法,调用父类被覆盖的show方法,增加打印自己的属性的语句,请实现Apple类的编写。-*/class Goods double unitPrice;/单价 double account;/数
19、量 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) this.unitPrice=unitPrice; this.account=account ; this.kind=kind; public void show() super.show(); System.out.println(类别为 + kind); /* E
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1