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();
}
}