在这里插入代码片设计】
---------------------------------------------------------
题目:使用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。
-------------------------------------------------------*/
public 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;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
/*-------------------------------------------------------
【程序设计】
---------------------------------------------------------
题目:使用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的初始值分别是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();
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 double 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**********/
private 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类的一个对象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 main(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 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();
shop.computeDiscout(0.2);
}
/********** End **********/
}
843
/*-------------------------------------------------------
【程序设计】
---------------------------------------------------------
题目:现有父类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**********/
String teacherID;
public Teacher()
{
this.name="Shampoo";
this.teacherID="23333";
}
public void print()
{
super.print();
System.out.println("teacherID =" + teacherID + ",name =" + name);
}
/********** End **********/
}
public class Prog1{
public static void main(String[] args) {
}
}
844
/*-------------------------------------------------------
【程序设计】
---------------------------------------------------------
题目:现有父类Person,在此基础上派生出子类Student,子类定义了自己的属性String类型的学号(studentID),
有不带参数的构造方法,覆盖了父类的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;
public Student()
{
this.name="Shampoo";
this.studentID="23333";
}
public void print()
{
super.print();
System.out.println("studentID =" + studentID + ",name =" + name);
}
/********** End **********/
}
public class Prog1{
public static void main(String[] args){
}
}
845
/*--------------------------重载和覆盖:
1.定义求绝对值的方法abs(),利用方法的重载实现既能求整数的绝对值, 又能求浮点数的绝对值。
public class Test1 {
public static abs(int n){
if(n<=0)
return -n;
else return n;
}
public static double (double n){
if(n<=0)
return -n;
else return n;
}
}
答案:
1) int
2)abs
2.利用方法重载定义平均值的方法average(),既能求三个整数的平均值,又能求两个整数的平均值。
public class Test2 {
public static average(int a,int b,int c){
return (a+b+c)/3.0;
}
public static double average(int a,int b){
return ;
}
}
答案:
1)double
2) (a+b)/2.0
4.在父类中定义了printA()方法,输出“A”,子类中覆盖printA()方法输出“AA”
class Father {
int a;
public void printA(){
System.out.println("A");
}
}
class Child extends Father{
printA(){
System.out.println("AA");
}
}
答案:
1)public
2) void
抽象类:
6.定义一个抽象类,包括能求面积的抽象方法
public class Test6 {
public abstract double area()
}
答案:
1)abstract
2) ;
接口
11.下面是定义一个接口ITF的程序,完成程序填空。
public _______ ITF
{
public static final double PI=Math.PI;
public abstract double area(double a, double b)
}
答案:
1)interface
2) ;
13.定义一个接口A,类B实现接口A,完成程序填空。
public A {
void a();
}
class B A{
public void a(){
}
}
答案:
1)interface
2) implements
字符串:
16. 编写一个方法isHuiWen(String str),检查一个字符串是否为回文。
提示:所谓回文是指该字符串从前读它和从后读它都是一样的。例如:”abcba”
public static boolean isHuiWen(String str){
for(int i=0;i< ;i++){
if(str.charAt(i)!=str.charAt(str.length()-1-i))
return false;
}
;
}
答案:
1)str.length()/2
2) return true
17. 编写一个方法isSubStr(String originalStr,String str){,判断一个字符串是否为另一个字符串的子串。
public static boolean isSubStr(String originalStr,String str){
if(originalStr. !=-1){
return true;
}
else ;
}
答案:
1)indexOf(str)
2) return false
18.编写方法int countNum(String str),该方法的功能是统计已知字符串str中数字的个数。例如countNum(“A23cd4r56d”)的返回值是5
public static int countNum(String str){
int c=0;
for(int i=0;i< ;i++){
if(str.charAt(i)>='0'&&str.charAt(i)<='9')
c++;
}
;
}
答案:
1)str.length()
2) return c
19. 编写一个方法isEqual(String str1,String str2),判断两个字符串是否相等。
public static boolean isEqual(String str1,String str2){
if(str1. (str2))
return true;
else ;
}
答案:
1)equals
2) return false
20. 编写程序在main方法中,将两个字符串s1和s2转换成对应的数值然后完成加法运算。
public class Test9{
public static void main(String[] args) {
String s1="2";
String s2="2.5";
int n1= ;
double n2= ;
double sum=n1+n2;
System.out.println(sum);
}
}
答案:
1) Integer.valueOf(s1)
2) Double.valueOf(s2)
第三章:类的定义和对象的创建
1.创建一个Circle类,此类中包括一个半径属性radius和一个计算面积的方法findArea。在main方法中创建Circle类的对象c,并计算半径为5的圆的面积。
public class Circle {
double radius;
double findArea(){
return 3.14*radius*radius;
}
public static void main(String[] args){
___ __________
c.radius=5.0;
System.out.println(_____________);
}
}
2. 创建一个Circle类,此类中包括一个半径属性radius和一个计算周长的方法findLong。在main方法中创建Circle类的对象c,并计算半径为10的圆的周长。
public class Circle {
double radius;
double findLong(){
return 2*3.14*radius;
}
public static void main(String[] args){
__________________
c.radius=10.0;
System.out.println(_______________);
}
}
3. 创建一个矩形Rectangle类,此类中包括2个属性:长度L和宽度D。一个计算面积的方法findArea。在main方法中创建Rectangle类的对象r,并计算长度为3,宽度为5的矩形面积。
public class Rectangle {
int L,D;
int findArea(){
return L*D;
}
public static void main(String[] args){
____________________
r.L=3;
r.D=5;
System.out.println(____________________);
}
}
第四章:构造方法和this的使用
1.创建一个Telephone类,属性有电话号码number,还有2个构造方法,其中一个没有参数,一个带参数。
public class Telephone {
String number;
__ Telephone() __________{
number="041184835202";
}
Telephone(String number){
___this.number=number;__________
}
java程序填空
最新推荐文章于 2023-04-07 20:15:35 发布