类,用于计算创建的对象数量 + 随机数生成器 Java

huangapple go评论67阅读模式
英文:

Class that counts the number of objects created + random number generator Java

问题

package ping;

import java.util.Random;

public class Ping {
    public static void main(String[] args) {
        Random rnd = new Random();

        for (int i = 0; i < 4; i++) {
            int r1 = rnd.nextInt(256);
            int r2 = rnd.nextInt(256);
            int r3 = rnd.nextInt(256);
            int r4 = rnd.nextInt(256);

            System.out.println(r1 + "." + r2 + "." + r3 + "." + r4);
        }
    }
}
英文:

Alright, complete noob here trying to learn how to code. Sorry in advance.

My mission is to create a class "Ping" that contains a class varieble that keeps track of how many "Ping-objects" I create. These Ping-objects should have 4 attributes whose value is randomly generated when the object is created. These values should be INTEGERS between 0-255. When these objects are created these attributes should also be printed formated as follow:

172.15.256.1
192.165.0.0
10.100.68.215

The only code I have thus far is this and I´m pretty sure it´s completely useless lol. Any help is greatly appreciated!

package ping;

import java.util.Random;

public class Ping {
    public static void main(String[] args) {
        Random rnd = new Random();


        System.out.println();
        for (int i = 0; i &lt; 4; i++) {
            int r1 = rnd.nextInt(255);
            int r2 = rnd.nextInt(255);
            int r3 = rnd.nextInt(255);
            int r4 = rnd.nextInt(255);



          System.out.println(r1 + &quot;.&quot; + r2 + &quot;.&quot; +r3 + &quot;.&quot; + r4 );```
```OUTPUT 

141.143.64.132

20.121.58.51

40.54.128.90

226.161.89.26```

</details>


# 答案1
**得分**: 2

以下是您提供的代码的翻译部分:

```java
public class Ping {
    int r1;
    int r2;
    int r3;
    int r4;
    
    public static int number = 0;
    
    Ping() {
        number++;
        Random rnd = new Random();
        r1 = rnd.nextInt(255);
        r2 = rnd.nextInt(255);
        r3 = rnd.nextInt(255);
        r4 = rnd.nextInt(255);
    }
    
    @Override
    public String toString() {
        return r1 + "." + r2 + "." + r3 + "." + r4;
    }
    
    public static void main(String[] args) {
        Ping ping1 = new Ping();
        Ping ping2 = new Ping();
        System.out.println("pings: " + Ping.number + "\np1: " + ping1 + "\np2: " + ping2);
    }
}

输出:

pings: 2
p1: 177.127.106.216
p2: 164.221.52.251
英文:

Well, you need to define the fields and constructor for the Ping class (consider adding getter methods if you need to retrieve the values). In order to find out how many pings you have created, you need to use a static field (a static field has the same value regardless of where it is accessed from and can be accessed using the class, as Ping.number, you don't necessarily need an instance of a Ping object). When you make a new Ping() object, the constructor is called, and all 4 fields are initialized randomly. Also, the number is incremented, so this is how you actually keep track of how many objected you created. Note that the toString method is used to convert a Ping object to a string, so it can be nicely printed to the console.

public class Ping {
    int r1;
    int r2;
    int r3;
    int r4;
    
    public static int number = 0;
    
    Ping () {
    	number++;
    	Random rnd = new Random();
    	r1 = rnd.nextInt(255);
        r2 = rnd.nextInt(255);
        r3 = rnd.nextInt(255);
        r4 = rnd.nextInt(255);
    }
    
    @Override
    public String toString() {
    	return r1 + &quot;.&quot; + r2 + &quot;.&quot; + r3 + &quot;.&quot; + r4;
    }
    
    public static void main(String[] args) {
        Ping ping1 = new Ping();
        Ping ping2 = new Ping();
        System.out.println(&quot;pings: &quot; + Ping.number + &quot;\np1: &quot; + ping1 + &quot;\np2: &quot; + ping2);
    }
}

Output:

pings: 2
p1: 177.127.106.216
p2: 164.221.52.251

答案2

得分: 1

使用新的 Java8 中的 ArraysStream 库,您可以通过更少的代码行获得更高性能的解决方案来处理您的类。此外,您需要重写 toString() 方法以便能够打印出您的 ping 对象。

import java.util.Random;
import java.util.stream.*;
import java.util.Arrays;

class Ping {
    private static int count = 0;
    private int[] arr = new int[4]; 

    Ping () {
        ++count;
        Random rnd = new Random();
        Arrays.setAll(arr, i -> rnd.nextInt(255));
    }

    public static int getCount() {
        return count;
    }

    @Override 
    public String toString() {
        return Arrays.stream(arr).mapToObj(String::valueOf).collect(Collectors.joining("."));
    }

    public static void main(String[] args) {
        Ping ping1 = new Ping();
        Ping ping2 = new Ping();
        System.out.println("Ping Count: " + Ping.getCount() + "\nPing1: " + ping1 + "\nPing2: " + ping2);
    }
}

输出:

Ping Count: 2
Ping1: 212.55.226.115
Ping2: 140.132.194.210
英文:

With the new Java8 Arrays and Stream libraries you can get a more performant solution for your class with less lines of code.
Also You need to override the toString() method to be able to print your ping objects.

import java.util.Random;
import java.util.stream.*;
import java.util.Arrays;

class Ping {
    private static int count = 0;
    private int[] arr = new int[4]; 
   // Declare them as an array instead of separate variables.

    Ping () {
        ++count;
        Random rnd = new Random();
        Arrays.setAll(arr, i -&gt; rnd.nextInt(255)); // similar to for(int i = ...)
    }

    public static int getCount() {
        return count;
    }
    // The stream collects all ints into a &#39;.&#39; separated string.
    @Override 
    public String toString() {
        return Arrays.stream(arr).mapToObj(String::valueOf).collect(Collectors.joining(&quot;.&quot;));
    }

    public static void main(String[] args) {
        Ping ping1 = new Ping();
        Ping ping2 = new Ping();
        System.out.println(&quot;Ping Count: &quot; + Ping.getCount() + &quot;\nPing1: &quot; + ping1 + &quot;\nPing2: &quot; + ping2);
    }
}

Output:

Ping Count: 2
Ping1: 212.55.226.115
Ping2: 140.132.194.210

huangapple
  • 本文由 发表于 2020年9月15日 22:56:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/63904542.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定