关于java平时作业题目2的信息

博主:adminadmin 2022-12-15 09:51:06 79

今天给各位分享java平时作业题目2的知识,其中也会对进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java程序分析题作业2

题目意思是问你要运行哪写步骤:

如果你输入的A,那么肯定捕获异常,因为要求输入的是整数

如果:你输入是A 会捕获InputMismatchException e

也就是会执行:

System.out.println("----Flag 3 ----");

System.out.println("输入数据格式错,要求是整型!");

System.out.println("----Flag 4 ----");

接下来是:

System.out.println("----Flag 5 ----");

System.out.println("执行完毕!");

System.out.println("----Flag 6 ----");

如果你输入的是3:

会执行下面这些:

打印一个C,不会越界,因为i-1 了, 3-1=2所以是C

System.out.println("----Flag 2 ----");

System.out.println("----Flag 5 ----");

System.out.println("执行完毕!");

System.out.println("----Flag 6 ----");

刚才修改了下输入3的后果,代码有点乱没仔细看,你还有一个数组存在

Java作业两道题

1.public class Complex {

int realPart ;//实数

int imaginPart;//虚数

public Complex(){

this.imaginPart = 0;

this.realPart = 0;

}

public Complex(int r ,int i ){

this.realPart = r;

this.imaginPart = i;

}

public Complex complexAdd(Complex a){

this.imaginPart += a.imaginPart;

this.realPart += a.realPart;

return this;

}

public String toString() {

if(imaginPart = 0){

return this.realPart+"+"+this.imaginPart+"i";

}

return this.realPart+"-"+this.imaginPart+"i";

}

public static void main(String[] args) {

Complex c = new Complex(1,3);

c.complexAdd(c);

System.out.println(c.toString());

}

}

2.public class Rectangle {

double length;

double width;

public Rectangle(double length,double width){

this.length = length;

this.width = width;

}

public double getArea(){

double a = this.length * this.width;

System.out.println("矩形面积为"+a);

return a;

}

public static void main(String[] args) {

Rectangle r = new Rectangle(1.5, 1.5);

r.getArea();

}

}

3.public class Cone {

Rectangle rect;

double height;

public Cone(Rectangle rect,double height){

this.rect = rect;

this.height = height;

}

public double getVolume(){

double a = (this.rect.getArea() * this.height)/3;

System.out.println("四棱锥的体积为:"+a);

return a;

}

public static void main(String[] args) {

Cone c = new Cone(new Rectangle(1.5, 1.5), 5);

c.getVolume();

}

}

多注重自己学习。祝你成功。

JAVA的2个作业题

1.

class MyPoint{

private int x,y;

public MyPoint(int x,int y){setX(x);setY(y);}//两参构造

public int getX(){

return this.x;

}

public void setX(int x){

this.x = x;

}

public int getY(){

return this.y;

}

public void setY(int y){

this.y = y;

}

public String toString(){

return "("+getX()+","+getY()+")";

}

}

public class Test{

public static void main(String[] args){

MyPoint mp = new MyPoint(1,2);

/*

或者

MyPoint mp = new MyPoint();

mp.setX(1);

mp.setY(2);

*/

System.out.println(mp.toString());

}

}

其实说是重写toString()方法会准确一点。

2.toString方法在1中已经写过了,这里又要增加这个方法?

与上同理,说是重写equals()和toString()方法会更好一点。因为这两个方法本身就已经存在,继承于Object类。

toString() 就如1所写了

public boolean equals(Object mp){

if(mp instanceof MyPoint this.getX()==mp.getX()this.getY()==mp.getY())

return true;

retrun false;

}

附加说明:为了保证哈希码的约定(最下面给出了),一般重写equals()之后也会相应的重写hashCode()方法。

public int hashCode(){

//根据hashCode常规约定的1和2条,只要返回一个由x和y共同决定的int数就行了

//比如

//return getX()+getY();

//return getX()*getY();

//但是为了哈希表的优化,至好是设计一个算法去附合常规约定 3的提义

//写下你的算法吧

}

附录:

hashCode 的常规协定是:

1.在 Java 应用程序执行期间,在同一对象上多次调用 hashCode 方法时,必须一致地返回相同的整数,前提是对象上 equals 比较中所用的信息没有被修改。从某一应用程序的一次执行到同一应用程序的另一次执行,该整数无需保持一致。

2.如果根据 equals(Object) 方法,两个对象是相等的,那么在两个对象中的每个对象上调用 hashCode 方法都必须生成相同的整数结果。

3.以下情况不 是必需的:如果根据 equals(java.lang.Object) 方法,两个对象不相等,那么在两个对象中的任一对象上调用 hashCode 方法必定会生成不同的整数结果。但是,程序员应该知道,为不相等的对象生成不同整数结果可以提高哈希表的性能。

java编程题作业二

public class Shuzu {

public static void main(String[] args) {

getMax();

getSum();

}

public static void getMax(){

int[] buf={10,15,12,9,7};

int max=0;

for (int i = 0; i  buf.length; i++) {

if(buf[i]max){

max=buf[i];

}

}

System.out.println("最大值:"+max);

}

public static void getSum(){

int[] buf={10,15,12,9,7};

int sum=0;

for (int i = 0; i  buf.length; i++) {

sum=sum+buf[i];

}

System.out.println("和为:"+sum);

}

}

java平时作业题目2的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于、java平时作业题目2的信息别忘了在本站进行查找喔。

The End

发布于:2022-12-15,除非注明,否则均为首码项目网原创文章,转载请注明出处。