编程.docx

上传人:b****6 文档编号:6576247 上传时间:2023-01-08 格式:DOCX 页数:20 大小:18.16KB
下载 相关 举报
编程.docx_第1页
第1页 / 共20页
编程.docx_第2页
第2页 / 共20页
编程.docx_第3页
第3页 / 共20页
编程.docx_第4页
第4页 / 共20页
编程.docx_第5页
第5页 / 共20页
点击查看更多>>
下载资源
资源描述

编程.docx

《编程.docx》由会员分享,可在线阅读,更多相关《编程.docx(20页珍藏版)》请在冰豆网上搜索。

编程.docx

编程

833

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

【程序设计】

---------------------------------------------------------

题目:

使用while循环和if语句实现计算并输出1-100的偶数和,循环变量名为i,存放和的变量名为sum

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

publicclassProg1{

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

publicstaticvoidmain(Stringargs[])

{

inti=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。

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

publicclassProg1{

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

publicstaticvoidmain(Stringargs[])

{

inti=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

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

publicclassProg1{

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

publicstaticvoidmain(Stringargs[])

{

inti;

for(i=1;i<=100;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

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

publicclassProg1{

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

publicstaticvoidmain(Stringagrs[])

{

inti,n=8,p=1;

for(i=1;i<=n;i++)

{

p*=i;

}

System.out.println(p);

}

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

}

837

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

【程序设计】

---------------------------------------------------------

题目:

使用while循环和if语句实现计算并输出1-100的奇数和,循环变量名为i,存放和的变量名为sum

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

publicclassProg1{

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

publicstaticvoidmain(Stringargs[])

{

inti=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的初始值分别是6和8;

3)方法:

计算并输出矩形的周长方法名为findPremeter();

4)创建一个Prog1类的对象,对象名为r,调用findPremeter方法。

 

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

publicclassProg1{

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

privatedoublewidth,height;

publicProg1()

{

this.width=6;

this.height=8;

}

publicdoublefindPremeter()

{

return2*(width+height);

}

publicstaticvoidmain(Stringargs[])

{

Prog1r=newProg1();

r.findPremeter();

}

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

}

839

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

【程序设计】

---------------------------------------------------------

题目:

属性有平时成绩(pingshi),期末成绩(qimo),都为int类型;不带参数的构造方法,方法有计算并输出总成绩的方法calculateScore(),

计算方式为:

总成绩=平时成绩+期末成绩的1/2;创建Prog1对象s,然后调用calculateScore()方法来输出总成绩。

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

publicclassProg1{

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

privateintpingshi,qimo;

publicProg1()

{

this.pingshi=40;

this.qimo=90;

}

publicdoublecalculateScore()

{

returnpingshi+qimo/2;

}

publicstaticvoidmain(Stringargs[])

{

Prog1s=newProg1();

System.out.println(s.calculateScore());

}

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

}

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

【程序设计】

---------------------------------------------------------

题目:

包含一个名为radius属性,类型为int,不带参数的构造方法和计算并输出面积方法findArea(面积=3.14*radius*radius),

创建Prog1类的一个对象c,调用findArea方法。

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

publicclassProg1{

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

privateintradius;

publicProg1()

{

this.radius=4;

}

publicdoublefindArea()

{

return3.14*radius*radius;

}

publicstaticvoidmain(Stringargs[])

{

Prog1c=newProg1();

c.findArea();

}

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

}

841

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

【程序设计】

---------------------------------------------------------

题目:

属性包括name(书名,String类型)、author(作者名,String类型)、price(书的价格,double类型),定义不带参数的构造方法,

定义输出图书基本信息的show方法。

创建Prog1类的一个对象b,调用show方法。

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

publicclassProg1{

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

privateStringname,author;

privatedoubleprice;

publicProg1()

{

this.name="CoreJava";

this.author="GaryCornell";

this.price=52.8;

}

publicvoidshow()

{

System.out.println("Thebooknameis"+name+",authoris"+author+",thepriceis"+price+".");

}

publicstaticvoidmain(Stringargs[])

{

Prog1b=newProg1();

b.show();

}

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

}

842

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

【程序设计】

---------------------------------------------------------

题目:

定义类,属性包括商品名称name(String)、商品编号id(String)和价格price(double)三个属性,

有无参的构造方法和计算折扣价格并输出的方法,方法头为publicvoidcomputeDiscout(doublepercent),其中形参代表打折的百分比。

创建商品类的对象,调用计算折扣价格的方法。

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

publicclassProg1{

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

privateStringname,id;

privatedoubleprice;

publicProg1()

{

this.name="ShenBao";

this.id="23333333";

this.price=52;

}

publicvoidcomputeDiscout(doublepercent)

{

this.price*=(1-percent);

}

publicstaticvoidmain(Stringargs[])

{

Prog1shop=newProg1();

puteDiscout(0.2);

}

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

}

843

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

【程序设计】

---------------------------------------------------------

题目:

现有父类Person,在此基础上派生出子类Teacher,子类定义了自己的属性String类型的教师编号(teacherID),

有不带参数的构造方法,覆盖了父类的print方法,调用父类被覆盖的print方法,增加打印自己的属性的语句,

请实现Teacher类的编写。

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

classPerson{

Stringid;

Stringname;

Person(Stringid,Stringname){

this.id=id;

this.name=name;

}

voidprint(){

System.out.println("id="+id+",name="+name);

}

}

classTeacherextendsPerson{

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

StringteacherID;

publicTeacher()

{

this.name="Shampoo";

this.teacherID="23333";

}

publicvoidprint()

{

super.print();

System.out.println("teacherID="+teacherID+",name="+name);

}

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

}

publicclassProg1{

publicstaticvoidmain(String[]args){

}

}

844

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

【程序设计】

---------------------------------------------------------

题目:

现有父类Person,在此基础上派生出子类Student,子类定义了自己的属性String类型的学号(studentID),

有不带参数的构造方法,覆盖了父类的print方法,调用父类被覆盖的print方法,增加打印自己的属性的语句,

请不实现Student类的编写。

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

classPerson{

Stringid;

Stringname;

Person(Stringid,Stringname){

this.id=id;

this.name=name;

}

voidprint(){

System.out.println("id="+id+",name="+name);

}

}

classStudentextendsPerson{

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

StringstudentID;

publicStudent()

{

this.name="Shampoo";

this.studentID="23333";

}

publicvoidprint()

{

super.print();

System.out.println("studentID="+studentID+",name="+name);

}

 

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

}

publicclassProg1{

publicstaticvoidmain(String[]args){

}

}

845

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

【程序设计】

---------------------------------------------------------

题目:

现有父类Good,在此基础上派生出子类Milk,子类定义了自己的属性double类型的会员价格(vipPrice),

有带参数的构造方法,覆盖了父类的show方法,调用父类被覆盖的show方法,增加打印自己的属性的语句,

请实现Milk类的编写

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

classGoods{

doubleunitPrice;//单价

doubleaccount;//数量

Goods(doubleunitPrice,doubleaccount){

this.unitPrice=unitPrice;

this.account=account;

}

doubletotalPrice(){//计算总价格

returnunitPrice*account;

}

voidshow(){

System.out.println("单价是"+unitPrice);

System.out.println("购买数量为"+account);

System.out.println("购买商品的总金额是"+this.totalPrice());

}

}

classMilkextendsGoods{

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

doublevipPrice;

publicMilk(doubleunitPrice,doubleaccount,doublevipPrice)

{

this.unitPrice=unitPrice;

this.account=account;

this.vipPrice=vipPrice;

}

publicvoidshow()

{

super.show();

System.out.println("会员价格为"+vipPrice);

}

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

}

publicclassProg1{

publicstaticvoidmain(String[]args){

}

}

846

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

【程序设计】

---------------------------------------------------------

题目:

现有父类Good,在此基础上派生出子类Apple,子类定义了自己的属性String类型的类别(kind),

有带参数的构造方法,覆盖了父类的show方法,调用父类被覆盖的show方法,增加打印自己的属性的语句,

请实现Apple类的编写。

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

classGoods{

doubleunitPrice;//单价

doubleaccount;//数量

Goods(doubleunitPrice,doubleaccount){

this.unitPrice=unitPrice;

this.account=account;

}

doubletotalPrice(){//计算总价格

returnunitPrice*account;

}

voidshow(){

System.out.println("单价是"+unitPrice);

System.out.println("购买数量为"+account);

System.out.println("购买商品的总金额是"+this.totalPrice());

}

classAppleextendsGoods{

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

Stringkind;

publicApple(doubleunitPrice,doubleaccount,Stringkind)

{

this.unitPrice=unitPrice;

this.account=account;

this.kind=kind;

}

publicvoidshow()

{

super.show();

System.out.println("类别为"+kind);

}

 

/**********E

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 幼儿教育

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1