小编LTH*_*LTH的帖子

Java约定:在类中使用getter/setter吗?

我的教授真的强调通过始终使用访问器和更改器访问私有实例变量来防止隐私泄露; 但是,我是否必须使用类中的类的getter/setter?

例如,如果我有以下类:

public class Person 
{
    private String name;
    private int age;
}
Run Code Online (Sandbox Code Playgroud)

我想为它编写一个toString()方法.我可以写一下:

public String toString()
{
    return name + " " + age;
}
Run Code Online (Sandbox Code Playgroud)

或者我需要做这样的事情:

public String toString()
{
    return this.getName() + " " + this.getAge();
}
Run Code Online (Sandbox Code Playgroud)

java coding-style

53
推荐指数
6
解决办法
2万
查看次数

Java- <T的含义扩展了Comparable <T >>?

完整的背景是:

public class RClass<T extends Comparable<T>>
Run Code Online (Sandbox Code Playgroud)

我是否正确地说标题中的语句意味着插入到方法中的参数必须是实现Comparable OR类派生类的类的对象?

谢谢.

java generics polymorphism

37
推荐指数
3
解决办法
7万
查看次数

如何在 Java 中使用枚举复制构造函数?

我正在尝试完成一个项目,尽管我已经尝试过,但我似乎无法做到这一点。这是枚举:

public enum Symbols {
    /**
     * The Seven
     */
    SEVEN(12,"images/seven.jpg"),
    /**
     * The Watermelon
     */
    WATERMELON(10,"images/watermelon.jpg"),
    /**
     * The Orange
     */
    ORANGE(8,"images/orange.jpg"),
    /**
     * The Plum
     */
    PLUM(6,"images/plum.jpg"),
    /**
     * The Lemon
     */
    LEMON(4,"images/lemon.jpg"),
    /**
     * The Cherry
     */
    CHERRY(2,"images/cherry.jpg");

    private int payout;             // Symbol payout value
    private BufferedImage image;    // Symbol image
    private String icon;            // Symbol file name

    /** Constructor - Payout must be positive and even, file name must not be null
     * @param payout - …
Run Code Online (Sandbox Code Playgroud)

java enums copy copy-constructor

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

什么是"异常控制"循环?

我正试图在我的学习指南上提出一个问题:

编写一个异常控制的循环,循环直到用户输入1到5之间的整数.

我无法破译这个问题的真正含义,因为我之前从未听过这个词,但这是我最好的猜测:

    Scanner input = new Scanner(System.in);
    int a = 0;

    while(a <= 0 || a > 5)
    {
        try {
            a = input.nextInt();

            if(a <= 0 || a > 5)
                throw new OutOfRangeException(); //my own Excpt. class
        } catch (OutOfRangeException e) {
            System.out.println(e.getMessage());
        }
    }
Run Code Online (Sandbox Code Playgroud)

你们觉得怎么样?我在这里错过了什么吗?

java loops exception

4
推荐指数
1
解决办法
6400
查看次数

对于大数字,素数因子分解算法失败

对于Project Euler的问题3,我遇到了一个奇怪的问题.该程序适用于其他小的数字,如13195,但当我试图处理像600851475143这样的大数字时,它会抛出此错误:

Exception in thread "main" java.lang.ArithmeticException: / by zero
    at euler3.Euler3.main(Euler3.java:16)
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

    //Number whose prime factors will be determined
    long num = 600851475143L;

    //Declaration of variables
    ArrayList factorsList = new ArrayList();
    ArrayList primeFactorsList = new ArrayList();

    //Generates a list of factors
    for (int i = 2; i < num; i++)
    {
        if (num % i == 0)
        {
            factorsList.add(i);
        }           
    }

    //If the integer(s) in the factorsList are divisable by any number between 1 
    //and the integer itself (non-inclusive), …
Run Code Online (Sandbox Code Playgroud)

java

2
推荐指数
1
解决办法
1347
查看次数

java"hello world"的简单ant文件

我正在尝试运行ant文件,并且我一直收到以下错误:

Exception in thread "main" java.lang.NoClassDefFoundError
Run Code Online (Sandbox Code Playgroud)

我的build.xml文件包含:

<project name="Proj0" default="compile" basedir=".">

  <description>
    Proj0 build file
  </description>

  <!-- global properties for this build file -->
  <property name="source.dir" location="src"/>
  <property name="build.dir" location="bin"/>
  <property name="doc.dir" location="doc"/>
  <property name="main.class" value="proj0.Proj0.java"/>

  <!-- set up some directories used by this project -->
  <target name="init" description="setup project directories">
    <mkdir dir="${build.dir}"/>
    <mkdir dir="${doc.dir}"/>
  </target>

  <!-- Compile the java code in ${src.dir} into ${build.dir} -->
  <target name="compile" depends="init" description="compile java sources">
    <javac srcdir="${source.dir}" destdir="${build.dir}"/>
  </target>

  <!-- execute the program …
Run Code Online (Sandbox Code Playgroud)

java ant

2
推荐指数
1
解决办法
1278
查看次数

项目欧拉,问题2- Java

我是一个相对较新的java程序员,而且我现在一直在修补这个程序,现在我已经陷入困境了.我希望你能帮助我.

所以该程序应该满足以下要求:

Fibonacci序列中的每个新术语都是通过添加前两个术语生成的.从1和2开始,前10个术语将是:

1,2,3,5,8,13,21,34,55,89 ......

通过考虑Fibonacci序列中的值不超过四百万的项,找到偶数项的总和.

这是我的代码:

    //Generates Fibonacci sequence
    while (fibNum < 144)
    {
        int lastValue = (Integer) fibList.get(fibList.size()-1);
        int secondToLastValue = (Integer) fibList.get(fibList.size()-2);

        fibNum = secondToLastValue + lastValue;

        if (fibNum < 144)
        {
            fibList.add(fibNum);
        }

    //Picks out the even numbers from limitFibList
    for (int i = 0; i < fibList.size(); i++)
    {
        if ((Integer) fibList.get(i) % 2 == 0)
        {
            evenNumsFibList.add(fibList.get(i));
        }
    }

    //Sums up the total value of the numbers in the evenNumsFibList
    for (int i = …
Run Code Online (Sandbox Code Playgroud)

java

1
推荐指数
2
解决办法
3985
查看次数

Java通过字符串搜索

所以我想搜索一个字符串,看它是否包含我正在寻找的子字符串.这是我写的算法:

    //Declares the String to be searched
    String temp = "Hello World?";

    //A String array is created to store the individual 
    //substrings in temp
    String[] array = temp.split(" ");

    //Iterates through String array and determines if the
    //substring is present
    for(String a : array)
    {
        if(a.equalsIgnoreCase("hello"))
        {
            System.out.println("Found");
            break;
        }
        System.out.println("Not Found");
    }
Run Code Online (Sandbox Code Playgroud)

这个算法适用于"你好",但我不知道如何让它为"世界"工作,因为它附有一个问号.

谢谢你的帮助!

java search

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

格式化字符串

所以我正在编写一个应该模拟飞机货物的类,而我在格式化类的toString()方法时遇到了麻烦.以下代码:

public class Cargo 
{
    private String label;
    private int weight, height, width, length;

    public String toString()
    {
        return String.format("%s%s\t\t%-7s%-2s%-5d%-11s%-2s%-3d%-2s%-3d%-2s%d",
                getLabel(), ":", "Weight", "=", getWeight(), "Dimensions",
                "=", getHeight(), "X", getWidth(), "X", getLength());
    }

    public static void main(String[] args)
    {
        Cargo cargo1 = new Cargo("Bill's Beans", 1200, 20, 15, 30);
        Cargo cargo6 = new Cargo("Chicago Bulls", 1000, 10, 10, 10);
        Cargo cargo7 = new Cargo("Lots of Meat", 3000, 20, 20, 20);
        Cargo cargo8 = new Cargo("Tammy's Tomatoes", 600, 6, 6, 6); …
Run Code Online (Sandbox Code Playgroud)

java

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

Java简单的循环不工作

我希望以下代码从33到11倒数,但我无法弄清楚为什么这不起作用.我确信当我终于得知答案时,我将会有一个荷马辛普森"d'哦的时刻,但是现在,我真的很感激任何帮助.

    for(int i = 33; i <= 11; i--)
    {
        System.out.println(i);
    }
Run Code Online (Sandbox Code Playgroud)

java loops for-loop

0
推荐指数
1
解决办法
1257
查看次数

Java try-throw-catch麻烦

我想执行一堆抛出异常的命令,但我遇到程序正常执行的问题.

这是我的代码的基本大纲:

try
{
   command1 //all three throw exceptions
   command2
   command3
}
catch (Exception e)
{
   //log the exception in a txt file and continue onto the next command
}
Run Code Online (Sandbox Code Playgroud)

如果command1抛出异常,我如何让我的程序移动到command2?换句话说,如何返回try-block并从中断处继续执行?谢谢.

java

0
推荐指数
1
解决办法
204
查看次数