您的位置:

java编程题,java编程题库

本文目录一览:

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

}

}