没有合适的资源?快使用搜索试试~ 我知道了~
1. 使用数组编程,按下列格式输出数字: 1. 设计一个复数类 complex,包括: 1. 声明一个 person 类,包含:
资源详情
资源评论
资源推荐
1. 使用数组编程,按下列格式输出数字:
1 3 6 10 15
2 5 9 14
4 8 13
7 12
11
public class Main
{
public static void main(String[] args)
{
int
a[][] = new int[][] { { 1, 3, 6, 10, 15 }, { 2, 5, 9, 14 }, { 4, 8, 13 }, { 7, 12 }, { 11 } };
for (int
i
= 0;
i
< a.length; i++)
{
for (int k = 0; k < a[i].length; k++)
{
System.out.print(a[i][k] +
"
");
}
System.out.println();
}
}
}
2. 定义一个学生类 (Student) ,属性有 private 的名字 (name), public 的年龄 (age);
设置 name 和 age 属性的方法 : setName(),getName(); setAge(),getAge(), 以及显示 name 和 ag e
值的方法 showName(),showAge();
编写 Application ,创建一个学生对象,设置 name 和 age 属性值,并显示 name 和 age.
public class student {
private String name;
public
int
age;
private void setName(String name)
{
this.name=name;
}
private void setAge(int age)
{
this.age=age;
}
public String getName()
{
return this.name;
}
public
int
getAge()
{
return this.age;
}
public void showName(String name)
{
System.out.println(name);
}
public void showAge(int age)
{
System.out.println(age);
}
}
public class Main {
public static void main(String[] args) {
student astudent=new student();
astudent.age=19;
astudent.name="wanghua";
astudent.showAge(astudent.getAge());
astudent.showName(astudent.getName());
}
}
1. 设计一个复数类 complex ,包括:
a. 成员变量:复数的实部和虚部 ;
b. 构造函数:带参数和不带参数的构造函数,对成员变量进行初始化;
c. 成员方法:复数的加、减、乘运算。
public class complex {
private double shibu;
private double xubu;
public complex(){
this(0.0,0.0);
}
public complex(double ashibu,double axubu)
{
shibu=ashibu;
xubu=axubu;
}
public double getshibu(){
return shibu;}
public double getxubu(){
return xubu;}
public void setshibu(double newshibu){
shibu=newshibu;}
public void setxubu(double newxubu){
xubu=newxubu;}
public void showcom(complex a){
if(a.xubu>0)
System.out.println(a.shibu+"+"+a.xubu+"i");
else
System.out.println(a.shibu+a.xubu+"i");
}
public complex comadd(complex a,complex b,complex c){
c.shibu=a.shibu+b.shibu;
c.xubu=a.xubu+b.xubu;
return c;
}
public complex comsub(complex a,complex b,complex c){
c.shibu=a.shibu-b.shibu;
c.xubu=a.xubu-b.xubu;
return c;
}
public complex commul(complex a,complex b,complex c){
c.shibu=(a.shibu*b.shibu)-(a.xubu*b.xubu);
c.xubu=(a.shibu*b.xubu)+(a.xubu*b.shibu);
return c;
}
public static void main(String[] args) {
complex a,b,c;
a=new complex(5.5,6.2);
b=new complex(10.1,3.4);
c=new complex(0,0);
System.out.print("a+b=");
c.comadd(a,b,c);
c.showcom(c);
System.out.print("a-b=");
c.comsub(a, b, c);
c.showcom(c);
System.out.print("a*b=");
c.commul(a, b, c);
c.showcom(c);
}
}
1 .定义一个 Point 类。该类具有以下特点:
a. int 型变量 x,y 表示屏幕坐标系上的一个点 ;
b. 两种构造方法实现对象的初始化: Point(int x,
int
y) 和 Point(Point p);
c. 计算两个 Point 对象之间距离的方法 distance(Point a, Point b);
d. 获得当前坐标 x,y 值的方法 getX(),getY();
e. 设置坐标 x,y 值的方法 setX(),setY();
f. 重写 Point 类的 toString() 方法,以格式 (x,y) 输出当前点的字符串 ;
g. 统计应用程序中 Point 类对象的个数(使用 Point 类的类变量 ) 。
package javaapplication1;
import static java.lang.Math.*;
public class Point {
private
int
x,y;
private static
int
count=0;
public Point()
{
this(0,0);
count++;
}
public Point(int a,
int
b)
{
x=a; y=b;
count++;
}
public
int
getx(){
return x;}
public
int
gety(){
return y;}
public
int
getcount()
{ return count;}
public void setx(int newx){
x=newx;}
public void sety(int newy){
y=newy;}
public static double distance(Point a, Point b){
return sqrt(pow((a.x-b.x),2)+pow((a.y-b.y),2));
}
public String toString()
{
return("("+x+","+y+")");
}
public static void main(String[] args) {
Point m,n;
m=new Point(2,3);
n=new Point(5,4);
m.getx();
m.gety();
n.getx();
n.gety();
System.out.println("the distance:"+ distance(m,n));
System.out.println(m);
System.out.println(n);
System.out.println(count);
}
}
1. 声明一个 person 类,包含:
a) 属性:姓名,年龄,性别;
b) 方法:设置 person 信息,显示 person 信息 ( 重写 toString 方法 ) ;
public class person
{
private String name,sex;
private int age;
public void set(String name,int age,String sex)
{
this.name=name;
this.sex=sex;
this.age=age;
}
public String getName(){return name;}
public int getAge(){return age;}
public String getSex(){return sex;}
public person()
{}
public person(String name,int age,String sex)
{
this.name=name;
this.sex=sex;
this.age=age;
}
public String toString()
{
return new String("His/Her name is:"+name+"\nHis/Her age
is:"+age+"\nHis/Her sex is:"+sex);
}
}
2. 声明一个 student 类,作为 person 类的子类。包含:
a) 属性:学号,英语成绩,数学成绩,物理成绩,平均成绩,总成绩;
b) 方法:设置学号,设置各科成绩,计算平均成绩,计算总成绩,显示 student 信息 ( 重
写 toString 方法 ) ;
public class student extends person{
private int number,EngGrade,MathGrade,PhyGrade,Aver,Total;
public student(){}
剩余23页未读,继续阅读
史努比狗狗
- 粉丝: 27
- 资源: 318
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- JAVA:RSA加密工具类
- 8145v 备份文件 8145v 备份文件
- Spring异步工具类
- 如何用Excel进行数据分析
- 基于Bert+BiLSTM+CRF的命名实体识别(高分项目).zip
- 财务自由操作系统课程十周课程笔记第四周
- 吉林大学计算机硕士研究生最优化理论期末自测AB卷
- RLHF Workflow: From Reward Modeling to Online RLHF
- You Only Cache Once: Decoder-Decoder Architectures for Language
- WAVCRAFT: AUDIO EDITING AND GENERATION WITH LARGE LANGUAGE MODEL
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0