java程序设计上机题,求答案

java程序设计上机题,求答案,第1张

public class Point {

int x

int y

public Point() {

}

public Point(int x, int y) {

this.x = x

this.y = y

}

public Point(int x) {

this.x = x

this.y = x

}

public double distance() {//求当前点到原点的距离

return Math.sqrt((x * x + y * y))

}

public double distance(int x1, int y1) {//求当前点到(x1,y1)的距离

return Math.sqrt((x-x1)*(x-x1) + (y-y1) * (y-y1))

}

public double distance(Point other){

int x2 = other.x

int y2 = other.y

return Math.sqrt((x-x2)*(x-x2) + (y-y2) * (y-y2))

}

}

1、package net.test

public class TestArray

{

int a[]={12,34,5,7,9,10}

public int max()

{

int j = a[1]

for(int i :a)

{

if(i >j)

{

j = i

}

}

return j

}

public long avrage()

{

long j = 0l

for(int i :a)

{

j = j + i

}

return j/a.length

}

}

2、package net.test

public class Person

{

String name

int age

Person(String name,int age)

{

this.name = name

this.age = age

}

public void printInfo()

{

System.out.println("name = "+this.name+" age = "+this.age)

}

}

package net.test

public class Student extends Person

{

String studentId

Student(String name, int age)

{

super(name, age)

}

Student(String name, int age,String studentId)

{

super(name, age)

this.studentId = studentId

}

public void printInfo()

{

System.out.println("name = "+this.name+" age = "+this.age+" StudentId =" + this.studentId)

}

}

3、package net.test

public class TestPrint

{

public static void main(String[] args)

{

for (int i = 1i<10i++)

{

for(int j = 0j<ij++)

{

System.out.print("*")

}

System.out.println()

}

}

}

此题的Employee类设计有缺陷,缺少员工薪水的属性。根据题意只能写出下面的程序:

abstract class Employee {

public abstract double earnings()

}

class YearWorker extends Employee {

@Override

public double earnings() {

return 0

}

}

class MonthWorker extends Employee {

@Override

public double earnings() {

return 0

}

}

class WeekWorker extends Employee {

@Override

public double earnings() {

return 0

}

}

class Company {

Employee[] employee

double salaries = 0

Company(Employee[] employee) {

this.employee = employee

}

public double salariesPay() {

salaries = 0

//计算salaries

for(int i=0i<employee.lengthi++) {

salaries += employee[i].earnings()

}

return salaries

}

}

public class HardWork {

public static void main(String[] args) {

Employee[] employee = new Employee[20]

for(int i=0i<employee.lengthi++) {

if(i % 3 == 0) {

employee[i] = new YearWorker()

}

else if(i % 3 == 1) {

employee[i] = new MonthWorker()

}

else if(i % 3 == 2) {

employee[i] = new WeekWorker()

}

}

Company company = new Company(employee)

System.out.println("公司年工资总额:" + company.salariesPay())

}

}

运行结果:

公司年工资总额:0.0


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/7830732.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-10
下一篇 2023-04-10

发表评论

登录后才能评论

评论列表(0条)

保存