1、编写一个无参方法,输出Hello。

package Dongruan;

public class ktlx1 {

public static void main(String[] args) {

print();

}

public static void print(){

System.out.println("hello!");

}

}

2、编写一个得到两个int型参数方法,输出两个参数的和。

package Dongruan;

public class ktlx1 {

public static void main(String[] args) {

int a=2;

int b=3;

print(a,b);

}

public static void print(int a,int b){

System.out.println(a+b);

}

}

3、编写一个得到两个int型参数方法,返回两个参数的和。

package Dongruan;

public class ktlx1 {

public static void main(String[] args) {

double a=print(2,3);

System.out.println(a);

}

public static  double  print(double a,double b){

return a+b;

}

}

1、编写一个方法,求整数n的阶乘,例如5的阶乘是1*2*3*4*5。 [必做题]

package Dongruan;

import java.util.Scanner;

public class ktlx1 {

public static void main(String[] args) {

int a=print(5);

System.out.println(a);

}

public static  int   print(int a){

int sum=1;

for(int i=1;i<=a;i++){

sum=sum*i;

}

return sum;

}

}

2、编写一个方法,判断该年份是平年还是闰年。[必做题]

package Dongruan;

import java.util.Scanner;

public class ktlx1 {

public static void main(String[] args) {

print(2020);

}

public static  int   print(int year){

if(year%4==0&&year%100!=0||year%400==0){

System.out.println("闰年");

}else{

System.out.println("平年");

}

return year;

}

}

1、编写一个方法,输出大于200的最小的质数。[选做题]

package Dongruan;

import java.util.Scanner;

import java.util.Scanner;

public class ktlx1 {

public static void main(String[] args) {

for(int i=201;i<300 ; i++){

if(i%2!=0&&i%3!=0&&i%5!=0&&i%7!=0&&i%9!=0){

System.out.println(i);

break;

}

}

}

}

2、写一个方法,功能:定义一个一维的int 数组,长度任意,然后将它们按从小到大的顺序输出(使用冒泡排序)(知识点:方法的定义和访问)。[选做题]

package Dongruan;

import java.util.Scanner;

public class CopyOfktlx2 {

public static void main(String[] args) {

int [] arr={12,3,56,1,0,10,56};

print(arr);

}

public static  void   print(int[] arr){

for(int i=1;i

for(int j=0;j

if(arr[j]

int temp=arr[j];

arr[j]=arr[j+1];

arr[j+1]=temp;

}

}

}

for(int i=1;i

System.out.println(arr[i]);

}

System.out.println("\n");

}

}

第六章

根据需求编写类:

西游记游戏软件中的游戏人物包括:

孙悟空:孙悟空的武器是金箍棒,战斗力五颗星,耐力五颗星

唐  僧:唐僧没有武器,战斗力为零,耐力五颗星

猪八戒:猪八戒的武器是耙子,战斗力四颗星,耐力两颗星

沙  僧:沙僧的武器是月牙铲 ,战斗力三颗星,耐力四颗星

要求根据需求抽象出属性和方法,编写类实现。

package Dongruan;

public class xiyouji {

private String weapon;

private String zdl;

private String nl;

private String name;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getWeapon() {

return weapon;

}

public void setWeapon(String weapon) {

this.weapon = weapon;

}

public String getZdl() {

return zdl;

}

public void setZdl(String zdl) {

this.zdl = zdl;

}

public String getNl() {

return nl;

}

public void setNl(String nl) {

this.nl = nl;

}

public void show1(){

System.out.println(getName()+"武器:"+getWeapon()+",战斗力:"+getZdl()+",耐力:"+getNl());

}

}

测试类

package Dongruan;

public class testxiyouji {

public static void main(String[] args) {

xiyouji x1=new xiyouji();

x1.setName("孙悟空      ");

x1.setWeapon("金箍棒");

x1.setZdl("五颗星");

x1.setNl("五颗星");

x1.show1();

xiyouji x2=new xiyouji();

x1.setName("唐僧         ");

x1.setWeapon("念经");

x1.setZdl("0");

x1.setNl("五颗星");

x1.show1();

xiyouji x3=new xiyouji();

x1.setName("猪八戒      ");

x1.setWeapon("九齿钉耙");

x1.setZdl("四颗星");

x1.setNl("二颗星");

x1.show1();

xiyouji x4=new xiyouji();

x1.setName("沙僧         ");

x1.setWeapon("月牙铲");

x1.setZdl("三颗星");

x1.setNl("四颗星");

x1.show1();

}

}

编写一个三角图形类,有三个属性分别代表三边长度。

编写属性要求如下:

边长必须为正数

三个边长必须能组合成三角形(三角形任意两边和大于第三边)

编写方法要求如下:

对边长进行赋值

输出三角形的三个边长

编写主函数,对该三角图形类进行调用。

package Dongruan;

public class sanjiaoxing {

int bc1;

int bc2;

int bc3;

public int getBc1() {

return bc1;

}

public void setBc1(int bc1) {

if(bc1<0){

System.out.println("你家边长能小于零?");

}

this.bc1 = bc1;

}

public int getBc2() {

return bc2;

}

public void setBc2(int bc2) {

if(bc2<0){

System.out.println("你家边长能小于零?");

}

this.bc2 = bc2;

}

public int getBc3() {

return bc3;

}

public void setBc3(int bc3) {

if(bc2<0){

System.out.println("你家边长能小于零?");

}

this.bc3 = bc3;

}

public void show(){

if((bc1+bc2)>bc3&&(bc2+bc3)>bc1&&(bc3+bc1)>bc2){

System.out.println("三边之和"+bc1+bc2+bc3);

System.out.println("bc1:"+bc1+"\nbc2:"+bc2+"\nbc3:"+bc3);

}

}

}

测试类

package Dongruan;

public class testsanjiaoxing {

public static void main(String[] args) {

sanjiaoxing s=new sanjiaoxing();

s.setBc1(0);

s.setBc2(3);

s.setBc3(5);

s.show();

}

}

main打印static 与非 static 区别

package Dongruan;

public class CircleStatic{

static double pi = 3.14;

int radius=100;

public static void main(String[ ] args){

System.out.println(pi);  //打印pi

CircleStatic c = new CircleStatic();

System.out.println(c.radius); //打印radius

}

}

1、 定义一个点类Point,包含2个成员变量x、y分别表示x和y坐标,2个构造器Point()和Point(int x0,y0),以及一个movePoint(int dx,int dy)方法实现点的位置移动,创建两个Point对象p1、p2,分别调用movePoint方法后,打印p1和p2的坐标。[必做题]

测试类

package Dongruan;

public class testpoint {

public static void main(String[] args) {

point p1=new point();

p1.movepoint(3, 4);

point p2=new point();

p2.movepoint(5, 8);

point p3=new point(12,34);

p3.point1();

}

}

package Dongruan;

public class point {

int x;

int y;

public point(){}

public point(int x0,int y0){

this.x=x0;

this.y=y0;

}

public void point1(){

System.out.println("无参构造("+x+","+y+")");

}

public void movepoint(int dx,int dy){

System.out.println("坐标是:("+dx+","+dy+")");

}

}

2、定义一个矩形类Rectangle: [必做题]

2.1  定义三个方法:getArea()求面积、getPer()求周长,showAll()分别在控制台输出长、宽、面积、周长。

2.2  有2个属性:长length、宽width

2.3  通过构造方法Rectangle(int width, int length),分别给两个属性赋值

2.4  创建一个Rectangle对象,并输出相关信息

package Dongruan;

public class rectangle {

int length;

int width;

public rectangle(int length,int width){

this.length=length;

this.width=width;

}

//面积

public void getarea(){

//return length*width;

System.out.println(length*width);

}

//周长

public void getper(){

//return 2*(length+width);

System.out.println(2*(length+width));

}

//showall

public void showall(){

System.out.println("长:"+length+",宽:"+width+"面积:"+length*width+"周长:"+2*(length+width));

}

}

测试类

package Dongruan;

public class testrectangle {

public static void main(String[] args) {

rectangle r=new rectangle(2, 3);

r.getarea();

r.getper();

r.showall();

}

}

3、定义一个笔记本类,该类有颜色(char)和cpu型号(int)两个属性。 [必做题]

3.1 无参和有参的两个构造方法;有参构造方法可以在创建对象的同时为每个属性赋值;

3.2  输出笔记本信息的方法

3.3  然后编写一个测试类,测试笔记本类的各个方法。

package Dongruan;

public class bijiben {

char color;

int cpu;

public bijiben(){}

public bijiben(char color,int cpu){

this.color=color;

this.cpu=cpu;

}

public  void print(){

System.out.println("笔记本信息: 颜色:"+color+",cpu型号"+cpu);

}

}

测试类

package Dongruan;

public class testbijiben {

public static void main(String[] args) {

bijiben b=new bijiben('黑',15);

b.print();

}

}

4、定义两个类,描述如下: [必做题]

4.1定义一个人类Person:

4.1.1定义一个方法sayHello(),可以向对方发出问候语“hello,my name is XXX”

4.1.2有三个属性:名字、身高、体重

4.2定义一个PersonCreate类:

4.2.1创建两个对象,分别是zhangsan,33岁,1.73;lishi,44,1.74

4.2.2分别调用对象的sayHello()方法。

package Dongruan;

public class person {

String name;

double height;

int weight;

int age;

public void sayhello(){

System.out.println("Hello!my name is "+name+",身高:"+height+",体重:"+weight+",年龄:"+age);

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public double getHeight() {

return height;

}

public void setHeight(double height) {

this.height = height;

}

public int getWeight() {

return weight;

}

public void setWeight(int weight) {

this.weight = weight;

}

}

测试类

package Dongruan;

public class testperson {

public static void main(String[] args) {

person p=new person();

p.setName("张三");

p.setAge(33);

p.setHeight(1.73);

p.setWeight(120);

p.sayhello();

}

}

5、定义两个类,描述如下: [必做题]

5.1定义一个人类Person:

5.1.1定义一个方法sayHello(),可以向对方发出问候语“hello,my name is XXX”

5.1.2有三个属性:名字、身高、体重

5.1.3通过构造方法,分别给三个属性赋值

5.2定义一个Constructor类:

5.2.1创建两个对象,分别是zhangsan,33岁,1.73;lishi,44,1.74

5.2.2分别调用对象的sayHello()方法

package Dongruan;

public class person {

String name;

double height;

double weight;

int age;

person(String name, double height, double d, int age) {

super();

this.name = name;

this.height = height;

this.weight = d;

this.age = age;

}

public void sayhello(){

System.out.println("Hello!my name is "+name+",身高:"+height+",体重:"+weight+",年龄:"+age);

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public double getHeight() {

return height;

}

public void setHeight(double height) {

this.height = height;

}

public double getWeight() {

return weight;

}

public void setWeight(int weight) {

this.weight = weight;

}

}

测试类

package Dongruan;

public class testperson {

public static void main(String[] args) {

person p=new person("张三",33,1.73,120);

p.sayhello();

}

}

1、 设计一个类Student,该类包括姓名、学号和成绩。设计一个方法,按照成绩从高到低的顺序输出姓名、学号和成绩信息。[选做题]

2、 package Dongruan;

3、

4、 public class student1 {

5、 private String name;

6、 private int id;

7、 private int score;

8、 public String getName() {

9、     return name;

10、         }

11、         public void setName(String name) {

12、            this.name = name;

13、         }

14、         public int getId() {

15、            return id;

16、         }

17、         public void setId(int id) {

18、            this.id = id;

19、         }

20、         public int getScore() {

21、            return score;

22、         }

23、         public void setScore(int score) {

24、            this.score = score;

25、         }

26、         public  student1(String name, int id, int score) {

27、            super();

28、            this.name = name;

29、            this.id = id;

30、            this.score = score;

31、         }

32、         public void show() {

33、         System.out.println("id:"+id+",name="+name+",score="+score);

34、         }

35、         public static void sort(student1[] stus){

36、            student1 stu;

37、            for(int i=0;i

38、                for(int j=0;j

39、                   if(stus[j].score>stus[j+1].score){

40、                       stu=stus[j];

41、                       stus[j]=stus[j+1];

42、                       stus[j+1]=stu;

43、                   }

44、                }

45、            }

46、         }

47、

48、

49、

50、     }

51、

测试类

package Dongruan;

public class teststudent {

public static void main(String[] args) {

student1[] stus=new student1[3];

stus[1]=new student1("zhangsan",1,88);

stus[0]=new student1("lisi",2,99);

stus[2]=new student1("wangwu",3,100);

student1.sort(stus);

for(student1 stu:stus){

stu.show();

}

}

}

2、定义一个汽车类Vehicle,要求如下:[选做题]

2.1属性包括:汽车品牌brand(String类型)、颜色color(String类型)和速度speed(double类型),并且所有属性为私有。

2.2至少提供一个有参的构造方法(要求品牌和颜色可以初始化为任意值,但速度的初始值必须为0)。

2.3为私有属性提供访问器方法。注意:汽车品牌一旦初始化之后不能修改。

2.4定义一个一般方法run(),用打印语句描述汽车奔跑的功能

2.5定义测试类VehicleTest,在其main方法中创建一个品牌为“benz”、颜色为“black”的汽车。

package Dongruan;

public class vehicle {

private String brand;

private String color;

private double speed;

public vehicle(String brand,String color){

this.brand=brand;

this.color=color;

}

vehicle(String brand, String color, double speed) {

super();

this.brand = brand;

this.color = color;

this.speed = speed;

}

public void run(){

System.out.println("这个汽车的品牌为"+this.brand+"这个汽车的颜色为"+this.color+"这个汽车的速度为"+this.speed);

}

}

测试类

package Dongruan;

public class testvehicle {

public static void main(String[] args) {

vehicle v=new vehicle("benz","black");

v.run();

vehicle v1=new vehicle("benz","black",13);

v1.run();

}

}

java面向对象相关选择题_java面向对象练习题一相关推荐

  1. java面向对象的教程_java面向对象入门教程

    java面向对象入门教程 Java 编程语言的风格十分接近C.C++语言.Java是一个纯的面向对象的程序设计语言,以下是小编为大家搜索整理的java面向对象入门教程,希望能给大家带来帮助!更多精彩内 ...

  2. java面对对象教学_Java面向对象程序设计教与学

    原标题:Java面向对象程序设计教与学 面向对象程序设计(Object Oriented Programming,OOP)主要研究如何从对象的角度出发构建程序单元以及程序开发机制,主要内容包括抽象的技 ...

  3. java公社博客_Java面向对象开发学习笔记(一)

    Java面向对象开发 共105课时 课时1 面向对象简介 面向对象是一种程序设计方法,但是并不是所有开发者都认同面向对象,因为很多开发者认为面向对象过于复杂,所以更多人愿意使用函数式编程. 面向对象的 ...

  4. java类的心得_java面向对象学习心得3篇

    日记网 >> 专题 java面向对象学习心得3篇 更新时间:2018/6/15 8:27:00  点击率:937  手机版 java面向对象学习心得3篇来自简单日记网精选推荐.在面向对象的 ...

  5. java面向对象 程序设计题_java面向对象程序设计练习题

    java面向对象程序设计练习题 [练习题]01.类的成员变量 猜数字游戏 一个类 A 有一个成员变量 v有一个初值100.定义一个类 对 A 类的成员变量 v 进行猜.如果大了则提示大了 小 ...

  6. java异常对象引用变量_Java面向对象编程-异常处理

    第九章 异常处理 异常情况会改变正常的流程,导致恶劣的后果,为了减少损失,应该事先充分预料所有可能出现的异常,然后采取以下措施: 首先考虑避免异常,彻底杜绝异常的发生:如果不能完全避免,则尽可能地减少 ...

  7. java红牛农场答案_Java面向对象程序设计实验指导与习题解答(21世纪高等学校计算机专业实用规划教材)...

    导语 <Java面向对象程序设计实验指导与习题解答>是<Java面向对象程序设计>(作者耿祥义,清华大学出版社出版,2010)的配套实验指导和习题解答,目的是通过一系列实验练习 ...

  8. java面向对象的多态_java面向对象(五)之多态

    多态 面向对象编程有三大特性:封装.继承.多态. 封装隐藏了类的内部实现机制,可以在不影响使用的情况下改变类的内部结构,同时也保护了数据.对外界而已它的内部细节是隐藏的,暴露给外界的只是它的访问方法. ...

  9. java面向对象编程 漫画_Java面向对象编程(一)

    由于常常将Java和C++面向对象编程的原则搞乱,所以这次把相关要点分别总结一下,本文主要总结Java面向对象编程. 面向对象编程的三大特性是:继承性(inheritance), 多态性(polymo ...

最新文章

  1. 微服务生态与 Spring Cloud Alibaba
  2. 2019年Java和JVM生态系统预测:OpenJDK将成为Java运行时市场领导者
  3. c#获取对象的唯一标识_Articy Importer Guide - 01 基本对象处理
  4. 如何将浮点型准确地转换成字符串
  5. Js中 call() 与 apply() exports
  6. WinForm 之Control.Invoke 和Control.BeginInvoke 方法的使用 Control 不能在创建它的 Thread 之外被调用。但可以通过 invoke 来保证 C
  7. 程序员远程办公需要面临哪些挑战?
  8. Virtual Studio 2013 每次加载程序(dll)缓慢的问题
  9. Python学习笔记之类(二)
  10. jenkins配置用户权限
  11. LeetCode979. 在二叉树中分配硬币
  12. 编写安全的驱动程序之验证驱动的调用者
  13. iPhone应用程序的启动过程
  14. 给定一个字符串,求第一个不重复的字符
  15. 淘宝商品详情API接口(商品描述信息查询接口)
  16. lbochs模拟器最新版_bochs模拟器最新版下载
  17. 论文研究结论怎么写?
  18. win10删除微软输入法,使用搜狗输入法
  19. Gulp 编译Less和Sass
  20. 关于何如在英伟达官网上下载历史驱动的方法

热门文章

  1. UVA11029 Leading and Trailing【快速模幂+数学】
  2. 【概率证明】—— sum and product rules of probability
  3. 基于梯度的权重更新优化迭代算法
  4. 材料的构成 —— 塑料
  5. telnet 的使用(ping 与 telnet)
  6. 图案、标签、logo
  7. 强悍的 Linux —— 强悍的 ls
  8. VS 2013 统一修改所有工程的目录配置(以 boost、opencv3 的安装为例)
  9. python画柱状图-Python绘制柱状图
  10. 从零开始学习python编程-Python3.5从零开始学 PDF 下载