【问题标题】:Java testing out a driver classJava 测试驱动程序类
【发布时间】:2015-09-27 20:39:31
【问题描述】:

我在使用这个程序时遇到了一些问题。我必须使用驱动程序类测试每种方法,但我似乎无法理解当参数是字符串时我应该做什么。

我有一个int 参数的示例,但该示例从未显示任何关于字符串参数以及如何转换的内容。使用null 使我的驱动程序类运行但放置int 或字符串不会。

我该怎么做才能正确转换它,以便它可以显示我在无参数构造函数中的任何内容?

public class StudentListing 
{

    private String name;
    private String number;

    public StudentListing(String n, String num)
    {
        name = n;
        number = num;
    }

    public StudentListing()
    {
        name = null;
        number = null;

    }

    public String toString()
    {
        return("Name is  " + name +
                "\nNumber is " + number + "\n");

    }

       public void show()
    {
        System.out.println(toString());
    }

    public StudentListing Clone()
    {
        StudentListing clone = new StudentListing (name, number);
        return clone;
    }

    public int compareTo(String targetKey)
    {
        return (name.compareTo(targetKey));
    }

    public void input()
    {
        name = JOptionPane.showInputDialog("Enter a name");
        number = JOptionPane.showInputDialog("Enter a number");
    }// end of StudentListing

}//end of class
public class StudentListingDriver 
{
    public static void main(String[] args)
    {

       StudentListing s1 = new StudentListing();
       StudentListing s2 = new StudentListing(null,null);
       System.out.println(s1);
       s1.input();

       StudentListing s3 = s2.clone();
       s1.show();
    }

}

【问题讨论】:

    标签: java string output driver


    【解决方案1】:
    1. 您可以使用默认值并使无参数构造函数调用另一个构造函数。
    2. 使用Integer.parseInt();String 转换为Integer
    3. 您还需要检查exceptions 的转换。

    像这样:

    public class StudentListing {
        private String name;
        private int number;
    
        public StudentListing(String n, int num) {
            name = n;
            number = num;
        }
    
        public StudentListing() {
            this("defaultName", 0);
        }
    
        public String toString() {
            return ("Name is  " + name + "\nNumber is " + number + "\n");
        }
    
        public void show() {
            System.out.println(toString());
        }
    
        public StudentListing Clone() {
            StudentListing clone = new StudentListing(name, number);
            return clone;
        }
    
        public int compareTo(String targetKey) {
            return (name.compareTo(targetKey));
        }
    
        public void input() {
            name = JOptionPane.showInputDialog("Enter a name");
            number = Integer
                    .parseInt(JOptionPane.showInputDialog("Enter a number"));
        }
    }
    

    测试者:

    public class StudentListingDriver {
        public static void main(String[] args) {
            StudentListing s1 = new StudentListing();
            StudentListing s2 = new StudentListing(null, 0);
            System.out.println(s1);
            s1.input();
            StudentListing s3 = s2.Clone();
            s1.show();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-22
      • 2017-03-09
      • 2010-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-31
      • 1970-01-01
      相关资源
      最近更新 更多