Java oop題1(重要5題):哪些是類,模擬學生類,聲明人類,形狀類,商品類,學生類

1.下面以下內(nèi)容哪些是類,哪些是對象?
出租車、1802班排名第一的學生、金水灣小區(qū)、張麗同學、正在收看直播的同學、車牌為J1111的公交車
類:出租車,正在收看直播的同學,張麗同學(因為不只一個人叫張麗)。對象:車牌為J1111的公交車,1802班排名第一的學生(我的個人理解:對象有“具體,唯一,存在”的特性)
2.使用程序模擬學生類,包含學生姓名,性別,成績,聲明介紹的方法來介紹自己。分別聲明張三,李四,王五三名學生。
package a;
public class student{//總結的步驟:1.先定義創(chuàng)建一個類
public? String name;
public? String sex;
public double chengji;
public? student(String name,String sex,double chengji){
this.name=name;
this.sex=sex;
this.chengji=chengji;
}
//介紹自己的方法
public void say(){
System.out.println("我的姓名為"+this.name+",性別為"+this.sex+",成績?yōu)?#34;+this.chengji);
}
public static void main(String[] args) {
student a=new student("張三","男",98.5);
student b=new student("李四","女",99.5);
student c=new student("王五","男",100);
a.say();
b.say();
c.say();
}
}

3.聲明商品類,包含商品名稱,價格,包含商品介紹的功能,在商品介紹中聲明局部變量保質期,打印介紹的方法


package a;
class shangpin{
public? String name;
public? String gongneng;
public double jiage;
public? shangpin(String name,String gongneng,double jiage){
this.name=name;
this.gongneng=gongneng;
this.jiage=jiage;
}
//介紹自己的方法
public void say1(){
String date="5年";//局部變量保質期
System.out.println("商品名稱為"+this.name+",功能為"+this.gongneng+",價格為"+
this.jiage+",保質期為"+date);
}
public void say2(){
String date="7年";
System.out.println("商品名稱為"+this.name+",功能為"+this.gongneng+",價格為"+
this.jiage+",保質期為"+date);
}
public void say3(){
String date="6個月";
System.out.println("商品名稱為"+this.name+",功能為"+this.gongneng+",價格為"+
this.jiage+",保質期為"+date);
}
public static void main(String[] args) {
shangpin a=new shangpin("手機","打電話",998);
shangpin b=new shangpin("電腦","打代碼",9999.9);
shangpin c=new shangpin("辣條","食用",2);
a.say1();
b.say2();
c.say3();
}
}

4.聲明人類,包含人物姓名,年齡,性別,存款屬性,并設置其get/set方法,在set方法中判斷姓名只能為2到3個字符,年齡為1-100歲,性別為男或女,存款為大于0小于100000,聲明介紹的方法,介紹自己的內(nèi)容。


package a;
class person {
private String name;
private double? cunkuan;
private String sex;
private int? age;
public String getName() {
return name;
}
public void setName(String name) {
if(name.length()>2&name.length()<3){
this.name = name;}
else{
System.out.println("姓名只能為2到3個字符");
}
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
if((sex.equals("男")||sex.equals("女"))
){
this.sex = sex;}
else{
System.out.println("性別為男或女");
}
}
public int getAge() {
return age;
}
public void setAge(int age) {
if(age>1&age<100){
this.age = age;}
else{
System.out.println("年齡為1-100歲");
}
}
public double getCunkuan() {
return cunkuan;
}
public void setCunkuan(double cunkuan) {
if(cunkuan>0&cunkuan<100000)
this.cunkuan = cunkuan;
}
public person(String name, String sex, int age,double? cunkuan) {
this.name = name;
this.sex = sex;
this.age = age;
this.cunkuan = cunkuan;
}
// 介紹自己的方法
public void say() {
System.out.println("我的姓名為" + this.name + ",性別為" + this.sex + ",年齡為"
+ this.age+",存款為"+this.cunkuan);
}
public static void main(String[] args) {
person a = new person("張三", "男", 18,10000);
person b = new person("李四", "女", 19,100000);
person c = new person("王五", "男",20,100000);
a.setName("123456");
a.setSex("蔡徐坤");
b.setAge(789);
c.setCunkuan(123456789);
a.say();
b.say();
c.say();
}
}


5.聲明商品類,包含商品的ID,名稱,價格屬性,設置get/set,包含一個介紹商品的方法,介紹方法包含一個商品ID參數(shù),分別實例化三件商品放入到對象數(shù)組中,接收用戶輸入的商品ID,判斷該數(shù)組是否包含該商品,如果有就打印該商品,沒有則不打印。
package a;
import java.util.Scanner;
class shangpin {
String name;
private String ID;
private double jiage;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getID() {
return ID;
}
public void setID(String iD) {
ID = iD;
}
public double getJiage() {
return jiage;
}
public void setJiage(double jiage) {
this.jiage = jiage;
}
private shangpin(String name, String ID, double jiage) {
this.name = name;
this.ID = ID;
this.jiage = jiage;
}
// 介紹自己的方法
public void say() {
System.out.println("商品名稱為" + this.name + ",ID為" + this.ID
+ ",價格為" +
this.jiage );
}
public static void main(String[] args) {
shangpin[] stdAry = new shangpin[3];
stdAry[0] = new shangpin("手機", "123", 998);
stdAry[1] = new shangpin("電腦", "456", 9999.9);
stdAry[2] = new shangpin("辣條", "789", 2);
for (int i = 0; i < stdAry.length; i++) {
stdAry[i].say();
}
System.out.println("輸入商品ID" );
Scanner sstring=new Scanner(System.in);
String ID=sstring.next();
if(ID.equals("123")
){
stdAry[0].say();
}
else if(ID.equals("456")
){
stdAry[1].say();
}
else if(ID.equals("789")
){
stdAry[2].say();
}
else {
System.out.println("無商品ID" );
}
}
}

/*6.聲明形狀類,形狀有正方形,長方形,重載其構造方法,如果傳入一個參數(shù)是正方形,2個參數(shù)是長方形,聲明計算形狀面積的方法,打印其面積*/
package a;
class chongzai {
int chang;
int kuan;
int bianchang;
public chongzai(int chang) {
this. chang=chang;
}
public chongzai(int chang,int kuan) {
this. chang=chang;
this. kuan=kuan;
}
/*或public chongzai(int longs, int wide) {
System. out. println("這是一個長方形");
countArea (longs, wide) ;
}*/
public static void countArea(int bianchang) {
System. out. println("這是一個正方形,面積:"+bianchang *bianchang);}
public static void countArea(int? chang, int kuan){
System. out. println("這是一個長方形,面積:"+ chang *kuan);
}
public static void main(String[] args) {
chongzai.countArea(2);
chongzai.countArea(2,3);
/*或countArea(2);
countArea(2,3);*/
}
}

/*7.聲明一個學生類,包含學生ID,姓名,年齡,班級屬性,
設置其get/set,設置其介紹自己的方法,聲明老師類,包含老師姓名屬性,
設置其get/set,包含一個上課的方法,方法需要傳入學生對象,根據(jù)傳入的對象打印學生的信息。*/
package a;
public class fist {
public static void main(String[] args) {
// TODO Auto- generated method stub
student s1 = new student(1, "張三", 20, 1901);
student s2 = new student(2, "李四", 22, 1902);
student s3 = new student(2, "詩書畫唱", 20, 2000);
Teacher t = new Teacher();
t.setName("戴");
t.goToClass(s1);
t.goToClass(s2);
t.goToClass(s3);
}
}
class student {
private int ID;// ID
private String name;// . 姓名
private int age;// 年齡
private int grade;// 班級
public int getID() {
return ID;
}
public void setID(int ID) {
this.ID = ID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
public student(int ID, String name, int age, int grade) {
// TODO Auto-generated constructor stub
this.ID=ID;
this.name=name;
this.age=age;
this.grade=grade;
}
// 自我介紹的方法
public void introduce() {
System.out.println("大家好!我是"+name);
}
}
class Teacher {
private String name;// 姓名
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
// .上課的方法
public void goToClass(student stu) {
stu.introduce();
}
}
