小编Jon*_*now的帖子

javac错误:"导入x"中的"包x不存在"

我正在尝试使用命令提示符使用以下命令编译我的java文件'check4PrimeTest.java':

javac -classpath.:junit.jar check4PrimeTest.java

我收到以下错误:

错误:包junit.framework不存在import junit.framework.*;

我不知道为什么我得到这个错误,因为我在我的程序中导入了junit.framework.*.

下面是我的代码:

package check4prime;
// check4PrimeTest.java

//Imports 
import junit.framework.*;

public class check4PrimeTest extends TestCase {

    //Initialize a class to work with. 
    private check4Prime check4prime = new check4Prime();

    //constructor 
    public check4PrimeTest (String name) { 
        super(name);
    }

    //Main entry point 
    public static void main(String[] args) {
        System.out.println("Starting test...");
        junit.textui.TestRunner.run(suite());
        System.out.println("Test finished...");
    } // end main() 

    //Test case 1 
    public void testCheckPrime_true() {
        assertTrue(check4prime.primeCheck(3));
    }

    //Test cases 2,3 
    public void testCheckPrime_false() {
        assertFalse(check4prime.primeCheck(0));
        assertFalse(check4prime.primeCheck(1000));
    }

    //Test …
Run Code Online (Sandbox Code Playgroud)

java junit compiler-errors

19
推荐指数
1
解决办法
9万
查看次数

三角绘制方法

draw(Graphics g)在使用Java中的方法绘制三角形时遇到问题.我可以像这样绘制一个矩形:

public void draw(Graphics g) {
    g.setColor(colorFill);
    g.fillRect(p.x, p.y, width, height);
    g.setColor(colorBorder);
    g.drawRect(p.x, p.y, width, height);
    drawHandles(g);
Run Code Online (Sandbox Code Playgroud)

其中p代表"形状的左上角".我如何以相同的方式绘制三角形?

有人能给我一个标准三角形的例子吗?

java swing awt graphics2d

18
推荐指数
1
解决办法
15万
查看次数

继承和接口

当涉及接口时,我一直在尝试理解继承.我想知道子类是如何创建的,如果它们遵循以下内容:

例如,假设我有:

  1. 一个实现接口I的超类
  2. 和一对扩展超类A的子类

我的问题

  1. 我是否必须在扩展A的所有子类中提供接口方法'q和r'的实现?

  2. 如果我不在子类中提供接口的实现,我是否必须使该子类成为抽象类?

  3. 任何子类都可以实现我吗?例如,C类扩展A实现I,这可能吗?即使它已经扩展了一个实现我的超类?

  4. 假设我没有从接口I提供方法r的实现,那么我将不得不制作超类A和抽象类!那是对的吗?

我的示例代码:

    //superclass
    public class A implements I{
    x(){System.out.println("superclass x");}
    y(){System.out.println("superclass y");}
    q(){System.out.println("interface method q");}
    r(){System.out.println("interface method r");}
    }

    //Interface
    public Interface I{
    public void q();
    public void r();
    }

    //subclass 1
    public class B extends A{
    //will i have to implement the method q and r?
    x(){System.out.println("called method x in B");}
    y(){System.out.println("called method y in B");}
    }

    //subclass 2
    public class C extends A{
    //will i have to implement the method …
Run Code Online (Sandbox Code Playgroud)

java inheritance interface subclass

10
推荐指数
1
解决办法
1万
查看次数