扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
1、编一个学生类(Student),其中包含以下内容:
公司主营业务:成都网站建设、网站建设、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。成都创新互联是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。成都创新互联推出安乡免费做网站回馈大家。属性:学号studentNo,姓名studentName,性别studentGender,年龄studentAge。
方法:构造方法,显示学号方法showNo(),显示姓名方法showName(),显示性别方法showSex(),显示年龄方法showAge(),修改年龄方法modifyAge()。
主类(S3_1)包含:主方法main(),在其中创建两个学生对象s1和s2并初始化,两个对象的属性自行确定,然后分别显示这两个学生的学号、姓名、性别、年龄,然后修改s1的年龄并显示修改后的结果。
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("学号为:"+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的相关信息:");
s1.showNo();
s1.showName();
s1.showSex();
s1.showAge();
System.out.println("s2的相关信息:");
s2.showNo();
s2.showName();
s2.showSex();
s2.showAge();
System.out.println("修改s1的年龄:");
s1.modifyAge();
System.out.println("修改之后s1年龄为:");
s1.showAge();
}
}
2、编写一个简单计算器类,拥有加减乘除等方法,每个方法都有2个操作数,同为double类型或同为整型,方法设计为重载,在测试类里通过键盘输入操作数,显示计算结果。
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;
}
//此处为方法的重载,方法名一致,参数类型不同
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("整型参数加法测试:");
System.out.println("加法结果为:"+caculator.add(caculator.x,caculator.y));
System.out.println("double型参数加法测试:");
System.out.println("加法结果为:"+caculator.add(caculator.m,caculator.n));
System.out.println("减法测试:");
System.out.println("减法结果为:"+caculator.subtraction(caculator.x,caculator.y));
System.out.println("乘法测试:");
System.out.println("乘法结果为:"+caculator.multi(caculator.x,caculator.y));
System.out.println("除法测试:");
System.out.println("除法结果为:"+caculator.divide(caculator.m,caculator.n));
}
}
3、设计一个日期类,定义类的构造方法对日期进行初始化,在toString()中将其输出格式定为“月/日/年”。最后,编写一个测试程序来测试所定义的日期类能否实现预定的功能。
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、设计一个分数类,分数的分子和分母用两个整型数表示,类所拥有的方法包括对分数进行加、减、乘、除等运算,以及输出分数的方法,输出分数的格式应该是:分子/分母。
在测试类中定义分数类对象,运算并输出运算结果。
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表示大公约数
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();//改对象用于保存运算结果
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)设计一个雇员类,属性包括:编号、姓名、年龄、职务、部门,要求合理选定属性类型;该雇员类还拥有统计出勤人数的功能,可以考虑为雇员类设计一个静态属性;方法包括:构造方法、输出信息的方法、签到方法;
(2)创建雇员类对象,统计雇员的出勤人数。
注意考虑属性和方法的访问权限,方法的功能,及main方法中如何实现要求统计的信息。
class Employee{
public int employees = 100; //雇员人数
//属性,这里采用数组进行存储雇员数据
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; //出勤人数
//无参构造
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("请输入职务:");
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("签到人数:"+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("签到结束~~~");
System.out.println("签到人员相关信息:");
employee.output();
}
}
6、设计一个电视机类,属性包括商品编号、开关状态、音量、频道等,同时设计一些方法对电视机的状态进行控制。例如,方法应包括开/关电视机、更换频道、提高/减小音量等。要求商品编号自动生成(可以考虑为电视机类设置一个管理商品编号的静态成员变量,或者专门设置一个编号管理类)。
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("电视机的状态是:"+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();
}
}
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流