英文:
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 < 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 + "." + r2 + "." +r3 + "." + 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 + "." + 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);
}
}
Output:
pings: 2
p1: 177.127.106.216
p2: 164.221.52.251
答案2
得分: 1
使用新的 Java8
中的 Arrays
和 Stream
库,您可以通过更少的代码行获得更高性能的解决方案来处理您的类。此外,您需要重写 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 -> rnd.nextInt(255)); // similar to for(int i = ...)
}
public static int getCount() {
return count;
}
// The stream collects all ints into a '.' separated string.
@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);
}
}
Output:
Ping Count: 2
Ping1: 212.55.226.115
Ping2: 140.132.194.210
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论