本文目錄一覽:
- 1、java入門編程題:某班有十位同學,請順序輸入十位同學的學號,保存在數組中,並輸出所有同學的學號?
- 2、java編程題目,求求大佬救救我
- 3、java編程題:請按照下列提示編寫一個泛型介面以及其實現類?
- 4、Java編程題: 編寫一個Student類,包含name和age屬性,提供有參構造方法?
- 5、Java編程題編寫一個Java Application程序包含Person類、Student(學?
java入門編程題:某班有十位同學,請順序輸入十位同學的學號,保存在數組中,並輸出所有同學的學號?
import java.util.Scanner;
public class Students {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String[] students=new String[10];
String No=null;
for (int i = 0; i 10 ; i++) {
System.out.println(“請輸入學號:”);
No=in.next();
students[i]=No;
}
System.out.println(“學號是:”);
for (String a:students) {
System.out.print(a+” “);
}
}
}
java編程題目,求求大佬救救我
這個題考察的是面向對象三大特性之一的繼承。
子類繼承父類。
項目結構如何所示:
Mobile 為父類,有一個屬性:mobilePhone 代表電話號碼。
有4個方法(功能):
1、獲取手機號碼:public String getMobilePhone(){}
2、存儲手機號碼:public void setMobilePhone(String mobilePhone) {}
3、撥打電話號碼:public void callOnMobilePhone(){}
4、掛斷電話:public void callOffPhone(){}
具體代碼如下所示:、
————————————–mobilePhone 開始————————————–
/**
* @author 馮修遠
* 創建一個第一代手機類,要求包含手機號碼信息,並包含獲取電話號碼,
* 存儲電話號碼、撥打電話號碼和掛斷電話等功能。並以此為父類,派生
* 出子類第二代手機類,增加拍照功能。以第二代手機類來生成對象並
* 模擬實現撥打電話、掛斷電話拍照等功能。
*/
public class Mobile {
//手機號碼
private String mobilePhone;
/**
* 獲取手機號碼
* @return
*/
public String getMobilePhone() {
return mobilePhone;
}
/**
* 存儲手機號碼
* @param mobilePhone
*/
public void setMobilePhone(String mobilePhone) {
this.mobilePhone = mobilePhone;
}
/**
* 撥打電話號碼
*/
public void callOnMobilePhone(){
System.out.println(“撥打電話號碼:”+mobilePhone);
}
/**
* 掛斷電話
*/
public void callOffPhone(){
System.out.println(“掛斷與:”+mobilePhone+”的通話”);
}
}
————————————–mobilePhone 結束————————————–
PhotoMobile 為子類或者叫派生類,繼承自父類:Mobile
同時也繼承了父類的4個方法,但父類的屬性因為我設置的是private,所以繼承不了。
PhotoMobile 的代碼如下圖所示:
最後一個類,也就是測試類,用於創建第二代手機的對象,並調用相應的功能,如下圖所示:
最終,程序的運行結果如下圖所示:
我是馮修遠,如果我的答案對您有幫助的話,請採納以幫助更多的人,如果還有其它的問題,也請關注我,私信我,謝謝!
java編程題:請按照下列提示編寫一個泛型介面以及其實現類?
Generic.java:
package com.example.demo;
public interface GenericT {
void get(T t);
}
GenericImpl.java:
package com.example.demo;
public class GenericImplT implements GenericT {
@Override
public void get(T t) {
}
}
Java編程題: 編寫一個Student類,包含name和age屬性,提供有參構造方法?
//*********************Student
import java.util.Objects;
public class Student {
public String name;
public int age;
Student(){}
Student(String name,int age){
this.name=name;
this.age=age;
}
public String toString(){//重寫toString()
return “name: “+name+” age: “+age;
}
public boolean equals(Object o) {//重寫equals()
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return age == student.age Objects.equals(name, student.name);
}
public int hashCode() {//重寫hashCode()
return Objects.hash(name)+age;
}
}
//********************StudentTest
import java.util.HashSet;
public class StudentTest {
public static void main(String[] args) {
HashSetStudent hs=new HashSet();
Student s1=new Student(“zs”,19);//s1
Student s2=new Student(“zs”,19);//s2
Student s3=new Student(“ls”,19);//s3
hs.add(s1);//加入集合
hs.add(s2);//
hs.add(s3);//
//s1,s2,equals()返回true,s2不會加入集合
for(Student it:hs) {//遍歷HasSet
System.out.println(it);
}
}
}
Java編程題編寫一個Java Application程序包含Person類、Student(學?
public class Person {
private String name;//姓名
private String sex;//性別
public void sayHello() {
System.out.println(“姓名:” + name);
System.out.println(“性別:” + sex);
}
public Person() {
}
public Person(String name, String sex) {
this.name = name;
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
public class Student extends Person {
private String num;//學號
private String school;//學校
public void sayHello() {
super.sayHello();
System.out.println(“學號:” + num);
System.out.println(“學校:” + school);
}
public Student(String num, String school) {
this.num = num;
this.school = school;
}
public Student(String name, String sex, String num, String school) {
super(name, sex);
this.num = num;
this.school = school;
}
public Student() {
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
}
public class Test {
public static void main(String[] args) {
Student stu1 = new Student();
stu1.setName(“張三”);
stu1.setSex(“男”);
stu1.setNum(“20211225001”);
stu1.setSchool(“北京大學”);
Student stu2 = new Student(“20211225002”, “北京大學”);
stu2.setName(“李四”);
stu2.setSex(“男”);
Student stu3 = new Student(“王五”, “女”, “20211225003”, “清華大學”);
Person person1 = new Person();
person1.setName(“趙六”);
person1.setSex(“女”);
Person person2 = new Person(“孫七”, “女”);
stu1.sayHello();
stu2.sayHello();
stu3.sayHello();
person1.sayHello();
person2.sayHello();
}
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/257721.html