"");
}
}
}
}
6. .中国特色社会主义的体制中有这样的现象:
地方省政府要坚持党的领导和按照国务院的指示进行安全生产。请编写一个java应用程序描述上述的体制现象。要求如下:
(1)该应用程序中有一个“党中央”接口:CentralPartyCommittee,
该接口中有个“坚持党的领导”方法:void partyLeader()。
(2)该应用程序中有一个“国务院”抽象类:StateCouncil,
该抽象类中有个“安全生产”的抽象方法:abstract void safetyInProduction()。(3)该应用程序中有一个“省政府”类:Province,
该类继承StateCouncil抽象类并且实现CentralPartyCommittee接口;
在实现partyLeader()方法时输出“我们各省人民一定坚持党的领导!”;
在重写safetyInProduction()方法时输出
“我们各省人民一定按照国务院的指示进行安全生产!”。
(4)该应用程序中有一个主类E,
在主类E的main方法中创建Province类的对象,来测试Province类的功能。
interface CentralPartyCommittee {
void partyLeader();
}
abstract class StateCouncil {
abstract void safetyInProduction();
}
class Province extends StateCouncil implements CentralPartyCommittee { public void partyLeader(){
"我们各省人民一定坚持党的领导!");
}
public void safetyInProduction(){
"我们各省人民一定按照国务院的指示进行安全生产!");
}
}
class E {
public static void main(String[] args) {
Province p = new Province();
();
();
}
}
7.按要求编写一个Java应用程序:
(1)定义一个类,描述一个矩形,包含有长、宽两种属性,和计算面积方法。(2)编写一个类,继承自矩形类,同时该类描述长方体,
具有长、宽、高属性,和计算体积的方法。
(3)编写一个测试类,对以上两个类进行测试,
创建一个长方体,定义其长、宽、高,输出其底面积和体积。
class Rect{
double length;
double width;
Rect(double length, double width){
= length;
= width;
}
double getArea(){
return length * width;
}
}
class ChangFangTi extends Rect{
double length;
double width;
double height;
ChangFangTi(double length, double width, double height){
super(length,width);
= length;
= width;
= height;
}
double getTj(){
return length * width * height;
}
}
class RectTest {
public static void main(String[] args) {
ChangFangTi cft = new ChangFangTi(10,20,5);
"长方体的底面积:" + ());
"长方体的体积:" + ());
}
}
.按要求编写一个Java应用程序:
(1)编写一个矩形类Rect,包含:
两个protected属性:矩形的宽width;矩形的高height。
两个构造方法:
1.一个带有两个参数的构造方法,用于将width和height属性初化;
2.一个不带参数的构造方法,将矩形初始化为宽和高都为10。
两个一般方法:
求矩形面积的方法area()。
求矩形周长的方法perimeter()。
(2)通过继承Rect类编写一个具有确定位置的矩形类PlainRect,其确定位置用矩形的左上角坐标来标识,包含:
添加两个属性:矩形左上角坐标startX和startY。
两个构造方法:
带4个参数的构造方法,
用于对startX、startY、width和height属性初始化;
不带参数的构造方法,
将矩形初始化为左上角坐标、长和宽都为0的矩形;
添加一个方法:
判断某个点是否在矩形内部的方法isInside(double x,double y)。
如在矩形内,返回true, 否则,返回false。
提示:点在矩形类是指满足条件:
x>=startX && x<=(startX+width) &&
y>=(startY-height) && y<= startY
(3)编写PlainRect类的测试程序,
创建一个左上角坐标为(10,10),长为20,宽为10的矩形对象;
计算并打印输出矩形的面积和周长;
判断点,13)是否在矩形内,并打印输出相关信息。
class Rect{
protected double width;
protected double height;
Rect(double width, double height){
= width;
= height;
}
Rect(){
width = 10;
height = 10;
}
double area(){
return width * height;
}
double perimeter(){
return (width + height) * 2;
}
}
class PlainRect extends Rect {
double startX,startY;
PlainRect(double startX,double startY,double width, double height){
= startX;
= startY;
= width;
= height;
}
PlainRect(){
startX = 0;
startY = 0;
width = 0;
height = 0;
}
boolean isInside(double x,double y){
boolean rtn1 = (x >= startX) && (x <=(startX + width));
boolean rtn2 = (y <= startY) && (y >=(startY - height));
return rtn1 && rtn2;
}
}
class PlainRectTest {
public static void main(String[] args) {
PlainRect rc = new PlainRect(10,10,20,10);
area());
boolean flg = ,13);
if (flg){
",13)在矩形内.");
}else {
",13)不在矩形内.");
}
}
}
8.编写一个类A,该类创建的对象可以调用方法f输出小写的英文字母表。
然后再编写一个A类的子类B,要求子类B必须继承类A的方法f(不允许重写),子类B创建的对象不仅可以调用方法f输出小写的英文字母表,而且可以调用子类新增的方法g输出大写的英文字母表。
最后编写主类C,在主类的main方法中测试类A与类B。
class A {
void f(){
"小写的英文字母表:");
for(char c='a';c<='z';c++) {
" "+c);
}
}
}
class B extends A{
void g(){
"大写的英文字母表:");
for(char c ='A';c<='Z';c++) {
" "+c);
}
}
}
class C{
public static void main(String args[]){
A a = new A();
();
B b=new B();
();
();
}
}
9. adb首先,编写一个类ChongZai,该类中有3个重载的方法void print();其次,再编写一个主类,来测试ChongZai类的功能。
class ChongZai {
void print(){
" print() ");
}
void print(int i){
" print(int i) ");
}
void print(char c){
" print(char c) ");
}
}
public class E {
public static void main(String[] args) { ChongZai cz = new ChongZai();
();
(1);
('a');
}
}
求和
interface Computer{
int computer(int n,int m);
}
class Add implements Computer{
public int computer(int n,int m){
return n+m;
}
}
class Substract implements Computer{
public int computer(int n,int m){
return n-m;
}
}
class Multiply implements Computer{
public int computer(int n,int m){
return n*m;
}
}
class Divide implements Computer{
public int computer(int n,int m){
return n/m;
}
}
class UseCompute{
public void useCom(Computer com,int one,int two){ int result=(one,two);
"结果是:"+result);
}
}
public class Test{
public static void mian(String[] args){
Add add=new Add();
Substract sub=new Substract();
Multiply mul=new Multiply();
Divide div=new Divide();
UseCompute uc=new UseCompute();
(add,25,5);
(sub,25,5);
(mul,25,5);
(div,25,5);
}
}
11.在控制台打印出所有大写字母
class Print{
void output(int x){
if(x==1){
for(char c=’A’;c<=’Z’;c++){
”);
}
}
if(x=2){
for(char c=’a’;c<=’z’;c++){
”);
}
}
}
}
class TestClass{
public static void main(String args[]){
print p=new Print();
(1) ;
(2);
}
}
12.编写一个java程序,求两个正整数的最大公约数。如果这两个正整数不在1到1000范
围内,就抛出一个自定义异常。
class MyException extends Exception {
String message;
MyException(int x,int y){
message=x+"或"+y+"不在1到1000之内";
}
public String toString(){
return message;
}
}
class Student{
public int getMaxCommonDivisor(int m,int n) throws MyException{ if(n<=0||m<=0||n>=1000||m>=1000){
MyException exception=new MyException(m,n);
throw exception;
}
if(mint temp=0;
temp=m;
m=n;
n=temp;
}
int r=m%n;
while(r!=0){
m=n;
n=r;
r=m%n;
return n;
}
}
class E{
public static void main(String args[]){ Student s = new Student();
try{
int z=(10,1001);
"最大公约数是:"+z);
}catch(MyException e){
}
}
}
13.逆序输出
import class ArrayTest{
public static void main(String[] args){ Scanner s=new Scanner;
int a[]=new int[5];
for(int i=1;i<=5;i++){
请输入第”+i+”数”);
int temp=();
a[i-1]=temp;
}
for(int j=4;J>=o;j--){
}
}
14.等于0的项去掉
public class ArrayTest{
public static void main(String[] args){
int a[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5};
int b[]=new int[];
int index=0;
for(int i=0;i<;i++){
if(a[i] !=0){
b[index]=a[i];
index++;
}
}
}
}
15.已知两个一维数组:a[]={3,4,5,6,7},b[]={1,2,3,4,5,6,7},把数组a与数组b对应的元素乘积再赋值给数组b,如:b[2]=a[2]*b[2].
class Test{
public static void main(String[] args){
int a[]={3,4,5,6,7};
int b[]={1,2,3,4,5,6,7};
int len=;
if(len>
len=;
int i;
for(i=0;ib[i]=a[i]*b[i];
}
for(i=0;i<;i++){
"b["+i+"]"="+b[i];
}
}
}
,具有属性半径
//圆形
class Circle{
double radius;
public circle(double radius){
=radius;
}
//面积
public double getArea(){
return *radius*radius;
}
}
//圆锥类
class Cone{
Circle bottom;
double height;
public Cone(circle bottom,double height){
=bottom;
=height;
}
double getCubage(){
3*
}
}
class TestCone{
public static void main(String []args){ Circle c=new Circle(3);
Cone t=new Cone(c,2);
半径为”++”圆锥的体积为”+())
}