import java.util.Scanner; class TDVector { private double x; private double y; public String toString() { return "("+this.x+","+this.y+")"; } /** 你所提交的代码将被嵌在这里(替换此行) **/ } public class Main { public static void main(String[] args) { TDVector a = new TDVector(); /* 无参构造,向量的x和y默认为0 */ Scanner sc = new Scanner(System.in); double x,y,z; x = sc.nextDouble(); y = sc.nextDouble(); z = sc.nextDouble(); TDVector b = new TDVector(x, y); /* 按照参数构造向量的x和y */ TDVector c = new TDVector(b); /* 按照向量b构造向量 */ a.setY(z); System.out.println(a); System.out.println(b); System.out.println(c); c.setX(z); a = b.add(c); System.out.println(a); System.out.println("b.x="+b.getX()+" b.y="+b.getY()); sc.close(); } }
时间: 2024-01-19 15:05:04 浏览: 23
请在TDVector类中添加以下代码:
```java
public TDVector() {
this.x = 0;
this.y = 0;
}
public TDVector(double x, double y) {
this.x = x;
this.y = y;
}
public TDVector(TDVector v) {
this.x = v.x;
this.y = v.y;
}
public double getX() {
return this.x;
}
public double getY() {
return this.y;
}
public void setX(double x) {
this.x = x;
}
public void setY(double y) {
this.y = y;
}
public TDVector add(TDVector v) {
TDVector result = new TDVector();
result.x = this.x + v.x;
result.y = this.y + v.y;
return result;
}
```
主要是实现了无参构造函数、按照参数构造向量的构造函数、按照向量构造向量的构造函数、获取和设置x、y坐标的方法以及向量相加的方法。在主函数中创建了3个向量a、b、c,分别通过不同的构造函数进行初始化,然后调用相应的方法进行操作,并输出结果。
相关问题
代码错误import java.util.Scanner; public class Main{ public static void main (String args[]){ Shape s=new Oval(a,b); System.out.println("The area of "+s.toString()+" is "+s.area()); System.out.println("The perimeterof "+s.toString()+" is "+s.perimeter()); }} class Shape{ abstract double area(); abstract double perimeter(); } class Oval extends Shape{ private double a; //长轴半径 private double b; //短轴半径 Oval(double a,double b){ Scanner scanner = new Scanner(System.in); this.a= scanner.nextDouble(); this.b= scanner.nextDouble(); } Oval(){ this.a=0; this.b=0; } double area(){ return Math.PI*a*b; } double perimeter(){ return 2*Math.PI*b+4*(a-b); //L=2πb+4(a-b) } public String toString() { return "Oval(a:" + this.a + ",b:" + this.b + ")"; } }
这段代码有两个问题:
1. 在创建 Oval 对象时,应该将 main 函数中的 a 和 b 作为参数传递进去,而不是从控制台读取输入。因此,Shape s=new Oval(a,b); 应该改为 Shape s=new Oval(3, 4);(3 和 4 可以替换为任意值)
2. 在 Shape 类中的 abstract 方法需要添加 abstract 关键字,因为 Shape 类是一个抽象类。
修改后的代码如下:
```java
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
double a = 3; // 长轴半径
double b = 4; // 短轴半径
Shape s = new Oval(a, b);
System.out.println("The area of " + s.toString() + " is " + s.area());
System.out.println("The perimeter of " + s.toString() + " is " + s.perimeter());
}
}
abstract class Shape {
abstract double area();
abstract double perimeter();
}
class Oval extends Shape {
private double a; // 长轴半径
private double b; // 短轴半径
Oval(double a, double b) {
this.a = a;
this.b = b;
}
Oval() {
this.a = 0;
this.b = 0;
}
double area() {
return Math.PI * a * b;
}
double perimeter() {
return 2 * Math.PI * b + 4 * (a - b); // L=2πb+4(a-b)
}
public String toString() {
return "Oval(a:" + this.a + ",b:" + this.b + ")";
}
}
```
代码错误import java.util.Scanner; public class Main{ public static void main (String args[]){ Scanner sc=new Scanner(System.in); double a=sc.nextDouble(); double b=sc.nextDouble(); Shape s=new Oval(a,b); System.out.println("The area of "+s.toString()+" is "+s.area()); System.out.println("The perimeterof "+s.toString()+" is "+s.perimeter()); }} class Shape{ abstract double area(); abstract double perimeter(); } class Oval extends Shape{ private double a; //长轴半径 private double b; //短轴半径 Oval(double a,double b){ this.a= a; this.b= b; } Oval(){ this.a=0; this.b=0; } double area(){ return Math.PI*a*b; } double perimeter(){ return 2*Math.PI*b+4*(a-b); //L=2πb+4(a-b) } public String toString() { return "Oval(a:" + this.a + ",b:" + this.b + ")"; } }
这段代码有一个错误,因为抽象类 Shape 中的方法没有被声明为抽象方法。在 Java 中,如果一个类有抽象方法,那么这个类必须被声明为抽象类。因此,你需要在 Shape 类中将 area() 和 perimeter() 方法声明为抽象方法,如下所示:
```
abstract class Shape {
abstract double area();
abstract double perimeter();
}
```
另外,你还需要将 Oval 类的构造函数声明为 public,否则在其他类中无法访问该构造函数。
修复后的代码如下:
```
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
double a = sc.nextDouble();
double b = sc.nextDouble();
Shape s = new Oval(a, b);
System.out.println("The area of " + s.toString() + " is " + s.area());
System.out.println("The perimeter of " + s.toString() + " is " + s.perimeter());
}
}
abstract class Shape {
abstract double area();
abstract double perimeter();
}
class Oval extends Shape {
private double a; //长轴半径
private double b; //短轴半径
public Oval(double a, double b) {
this.a = a;
this.b = b;
}
public Oval() {
this.a = 0;
this.b = 0;
}
@Override
double area() {
return Math.PI * a * b;
}
@Override
double perimeter() {
return 2 * Math.PI * b + 4 * (a - b); //L=2πb+4(a-b)
}
public String toString() {
return "Oval(a:" + this.a + ",b:" + this.b + ")";
}
}
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)