1、編一個學(xué)生類(Student),其中包含以下內(nèi)容:
公司主營業(yè)務(wù):成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè)、移動網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。成都創(chuàng)新互聯(lián)是一支青春激揚、勤奮敬業(yè)、活力青春激揚、勤奮敬業(yè)、活力澎湃、和諧高效的團隊。公司秉承以“開放、自由、嚴謹、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團隊有機會用頭腦與智慧不斷的給客戶帶來驚喜。成都創(chuàng)新互聯(lián)推出安鄉(xiāng)免費做網(wǎng)站回饋大家。屬性:學(xué)號studentNo,姓名studentName,性別studentGender,年齡studentAge。
方法:構(gòu)造方法,顯示學(xué)號方法showNo(),顯示姓名方法showName(),顯示性別方法showSex(),顯示年齡方法showAge(),修改年齡方法modifyAge()。
主類(S3_1)包含:主方法main(),在其中創(chuàng)建兩個學(xué)生對象s1和s2并初始化,兩個對象的屬性自行確定,然后分別顯示這兩個學(xué)生的學(xué)號、姓名、性別、年齡,然后修改s1的年齡并顯示修改后的結(jié)果。
class Student{
Scanner in = new Scanner(System.in);
public String studentNo;
public int studentAge;
public String studentName;
public String studentGender;
public Student(String studentNo, int studentAge, String studentName, String studentGender) {
this.studentNo = studentNo;
this.studentAge = studentAge;
this.studentName = studentName;
this.studentGender = studentGender;
}
void showNo(){
System.out.println("學(xué)號為:"+studentNo);
}
void showName(){
System.out.println("姓名為:"+studentName);
}
void showSex(){
System.out.println("性別為:"+studentGender);
}
void showAge(){
System.out.println("年齡為:"+studentAge);
}
//修改年齡
void modifyAge(){
int age;
System.out.println("請輸入你要修改的年齡:");
age = in.nextInt();
studentAge = age;
System.out.println("修改成功");
}
}
public class S3_1 {
public static void main(String[] args) {
Student s1 = new Student("001",19,"spraing","男");
Student s2 = new Student("002",20,"段boy","男");
System.out.println("s1的相關(guān)信息:");
s1.showNo();
s1.showName();
s1.showSex();
s1.showAge();
System.out.println("s2的相關(guān)信息:");
s2.showNo();
s2.showName();
s2.showSex();
s2.showAge();
System.out.println("修改s1的年齡:");
s1.modifyAge();
System.out.println("修改之后s1年齡為:");
s1.showAge();
}
}
2、編寫一個簡單計算器類,擁有加減乘除等方法,每個方法都有2個操作數(shù),同為double類型或同為整型,方法設(shè)計為重載,在測試類里通過鍵盤輸入操作數(shù),顯示計算結(jié)果。
class Caculator {
int x,y;
double m,n;
public Caculator(int x, int y, double m, double n) {
this.x = x;
this.y = y;
this.m = m;
this.n = n;
}
public Caculator(int x, int y) {
this.x = x;
this.y = y;
}
public int add(int x, int y){
return x+y;
}
//此處為方法的重載,方法名一致,參數(shù)類型不同
public double add (double m, double n){
return m+n;
}
public int subtraction(int x, int y){
return x-y;
}
public int multi(int m, int n){
return x*y;
}
public double divide(double m, double n){
return m / n;
}
}
public class S3_2 {
public static void main(String[] args) {
Caculator caculator = new Caculator(12,15, 1.0, 4.0);
System.out.println("整型參數(shù)加法測試:");
System.out.println("加法結(jié)果為:"+caculator.add(caculator.x,caculator.y));
System.out.println("double型參數(shù)加法測試:");
System.out.println("加法結(jié)果為:"+caculator.add(caculator.m,caculator.n));
System.out.println("減法測試:");
System.out.println("減法結(jié)果為:"+caculator.subtraction(caculator.x,caculator.y));
System.out.println("乘法測試:");
System.out.println("乘法結(jié)果為:"+caculator.multi(caculator.x,caculator.y));
System.out.println("除法測試:");
System.out.println("除法結(jié)果為:"+caculator.divide(caculator.m,caculator.n));
}
}
3、設(shè)計一個日期類,定義類的構(gòu)造方法對日期進行初始化,在toString()中將其輸出格式定為“月/日/年”。最后,編寫一個測試程序來測試所定義的日期類能否實現(xiàn)預(yù)定的功能。
class Date{
public int year;
public int month;
public int day;
public Date(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
//toString()方法重寫
@Override
public String toString() {
return month + "/" + day + "/" + year;
}
}
public class S3_3 {
public static void main(String[] args) {
Date date = new Date(2022, 11, 13);
System.out.println(date.toString());
}
}
4、設(shè)計一個分數(shù)類,分數(shù)的分子和分母用兩個整型數(shù)表示,類所擁有的方法包括對分數(shù)進行加、減、乘、除等運算,以及輸出分數(shù)的方法,輸出分數(shù)的格式應(yīng)該是:分子/分母。
在測試類中定義分數(shù)類對象,運算并輸出運算結(jié)果。
class grade{
public int molecular; //分子
public int denominator; //分母
public static grade result = new grade();//在類里面定義自身為一屬性
public grade() {
}
public grade(int molecular, int denominator) {
this.molecular = molecular;
this.denominator = denominator;
}
//約分操作
void reduction(grade result){
int num = 0;//num表示大公約數(shù)
if (result.molecular >result.denominator && result.molecular % result.denominator == 0 && result.denominator != 1){
num = result.molecular / result.denominator;
result.molecular /= num;
result.denominator /= num;
}
if (result.molecular< result.denominator && result.denominator % result.molecular == 0 && result.molecular != 1){
num = result.denominator / result.molecular;
result.molecular /= num;
result.denominator /= num;
}
if (result.molecular == result.denominator){
result.denominator = 1;
result.molecular = 1;
}
}
//加法
void add(grade g1, grade g2){
result.molecular = g1.molecular*g2.denominator + g1.denominator*g2.molecular;
result.denominator = g1.denominator* g2.denominator;
reduction(result);
}
//減法
void subtract(grade g1, grade g2){
result.molecular = g1.molecular*g2.denominator - g1.denominator*g2.molecular;
result.denominator = g1.denominator* g2.denominator;
reduction(result);
}
//乘法
void multiply(grade g1, grade g2){
result.molecular = g1.molecular* g2.molecular;
result.denominator = g1.denominator* g2.denominator;
reduction(result);
}
//除法
void divided(grade g1, grade g2){
result.molecular = g1.molecular * g2.denominator;
result.denominator = g1.denominator * g2.molecular;
reduction(result);
}
}
public class S3_4 {
public static void main(String[] args) {
grade G = new grade();//改對象用于保存運算結(jié)果
grade g1 = new grade(1,2);
grade g2 = new grade(1,3);
G.add(g1, g2);
System.out.println("加法:"+G.result.molecular + "/" + G.result.denominator);
G.subtract(g1,g2);
System.out.println("減法:"+G.result.molecular + "/" + G.result.denominator);
G.multiply(g1,g2);
System.out.println("乘法:"+G.result.molecular + "/" + G.result.denominator);
G.divided(g1,g2);
System.out.println("除法:"+G.result.molecular + "/" + G.result.denominator);
}
}
5、(1)設(shè)計一個雇員類,屬性包括:編號、姓名、年齡、職務(wù)、部門,要求合理選定屬性類型;該雇員類還擁有統(tǒng)計出勤人數(shù)的功能,可以考慮為雇員類設(shè)計一個靜態(tài)屬性;方法包括:構(gòu)造方法、輸出信息的方法、簽到方法;
(2)創(chuàng)建雇員類對象,統(tǒng)計雇員的出勤人數(shù)。
注意考慮屬性和方法的訪問權(quán)限,方法的功能,及main方法中如何實現(xiàn)要求統(tǒng)計的信息。
class Employee{
public int employees = 100; //雇員人數(shù)
//屬性,這里采用數(shù)組進行存儲雇員數(shù)據(jù)
public String[] ID = new String[employees]; //編號
public String[] name = new String[employees]; //姓名
public String[] age = new String[employees]; //年齡
public String[] position = new String[employees]; //職位
public String[] department = new String[employees]; //部門
public int attendNum = 0; //出勤人數(shù)
//無參構(gòu)造
public Employee() {
}
//簽到方法
public void sign(){
for (int i = 0; i< employees; ++i){
Scanner in = new Scanner(System.in);
attendNum = i+1;
System.out.println("請輸入編號:");
ID[i] = in.nextLine();
System.out.println("請輸入姓名:");
name[i] = in.nextLine();
System.out.println("請輸入年齡:");
age[i] = in.nextLine();
System.out.println("請輸入職務(wù):");
position[i] = in.nextLine();
System.out.println("請輸入部門:");
department[i] = in.nextLine();
System.out.println("如果你是最后一個簽到的請輸入0,否則輸入1:");
int j = in.nextInt();
System.out.println("j:"+j);
if (j == 0){
break;
}
}
}
//輸出信息方法
public void output(){
System.out.println("簽到人數(shù):"+attendNum);
for (int i = 0; i< attendNum; ++i){
System.out.println("編號:"+ID[i]+" "+"姓名:"+name[i]+" "+"年齡:"+age[i]+" "+"職位:"+position[i]+" "+"部門:"+department[i]);
}
}
}
public class S3_5 {
public static void main(String[] args) {
Employee employee = new Employee();
System.out.println("開始簽到~~~");
employee.sign();
System.out.println("簽到結(jié)束~~~");
System.out.println("簽到人員相關(guān)信息:");
employee.output();
}
}
6、設(shè)計一個電視機類,屬性包括商品編號、開關(guān)狀態(tài)、音量、頻道等,同時設(shè)計一些方法對電視機的狀態(tài)進行控制。例如,方法應(yīng)包括開/關(guān)電視機、更換頻道、提高/減小音量等。要求商品編號自動生成(可以考慮為電視機類設(shè)置一個管理商品編號的靜態(tài)成員變量,或者專門設(shè)置一個編號管理類)。
class TV {
Random random = new Random();
public int ID = random.nextInt(10000); //商品編號
public static int channel = 28; //當前電視頻道
public static int voice = 50; //當前電視聲音
public static String status = "OFF";
public TV() {
}
static void changechannel(int num){
channel = num;
}
static String tvstatus(){
if (status == "OFF"){
status = "ON";
return status;
}
status = "OFF";
return status;
}
static void addvoice(){
voice++;
}
static void lowervoice(){
voice--;
}
public void showmessage(){
System.out.println("電視機的編號是:"+ID);
System.out.println("電視機的狀態(tài)是:"+status);
System.out.println("電視機的頻道是:"+channel);
System.out.println("電視機的聲音是:"+voice);
}
}
public class S3_6 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
TV tv = new TV();
tv.showmessage();
int num;
System.out.println("開電視......");
tv.tvstatus();
System.out.println("換臺........");
System.out.println("請輸入你想換的頻道:");
num = in.nextInt();
tv.changechannel(num);
System.out.println("加聲音......");
tv.addvoice();
System.out.println("最終電視的信息為:");
tv.showmessage();
}
}
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧