Math 클래스 :: random()
public static double random() {
return RandomNumberGeneratorHolder.randomNumberGenerator.nextDouble();
}
String toString() 은 Overriding으로 인해 적지 않았다.
class Box {
private String conts;
Box(String cont) {this.conts = cont;}
}
public class JavaPractice {
public static void main(String[] args) {
Box[] ar = new Box[3];
ar[0] = new Box("First");
ar[1] = new Box("Second");
ar[2] = new Box("Third");
for(int i=0; i<ar.length; i++) {
System.out.println(ar[i]);
}
}
}
[Console]
Box@4926097b
Box@762efe5d
Box@5d22bbb7
public class JavaPractice {
public static void main(String[] args) {
String[] sr = new String[7];
sr[0] = new String("Java");
sr[1] = new String("System");
sr[2] = new String("Compiler");
sr[3] = new String("Park");
sr[4] = new String("Tree");
sr[5] = new String("Dinner");
sr[6] = new String("Brunch Cafe");
int cnum = 0;
for(int i = 0; i< sr.length; i++) {
cnum += sr[i].length();
}
System.out.println("문자의 총 개수 : " + cnum);
}
}
[Console]
문자의 총 개수 : 43
class Circle {
private final double PI = 3.14;
// Math.PI로 사용할 수도 있다.
private double r;
Circle(double r) { // 생성자로 반지름의 값을 setting하기
this.r = r;
}
public double getArea() { // Circle의 넓이를 구하는 함수
return PI * r * r;
}
// 배열들의 합을 계산하기
public static double getArrArea(Circle[] circleArr) {
double sumArea = 0;
for (int i = 0; i < circleArr.length; i++) {
sumArea += circleArr[i].getArea();
// 배열에 있는 Circle을 가져와서 넓이를 구하고 sumArea에 더하기
}
return sumArea;
}
}
public class JavaPractice {
public static void main(String[] args) {
Circle[] circleArr = new Circle[2];
circleArr[0] = new Circle(10);
circleArr[1] = new Circle(10);
double areas = Circle.getArrArea(circleArr);
// class 이름으로 접근하는 함수 : static
System.out.println(areas);
}
}
class Rectangle {
private double width, height;
Rectangle(double width, double height){
this.width = width;
this.height = height;
}
public double getArea() {
return width * height;
}
public static double getArrArea(Rectangle[] recArr) {
double sumArea = 0;
for (int i = 0; i < recArr.length; i++) {
sumArea += recArr[i].getArea();
}
return sumArea;
}
}
public class JavaPractice {
public static void main(String[] args) {
Rectangle[] recArr = new Rectangle[2];
recArr[0] = new Rectangle(10, 20);
recArr[1] = new Rectangle(10, 30);
double areas2 = Rectangle.getArrArea(recArr);
System.out.println(areas2);
}
}
3-2에 이어서
import java.util.Scanner;
class Circle {
private final double PI = 3.14;
private double r;
Circle(double r) { // 생성자로 반지름의 값을 setting하기
this.r = r;
}
public double getArea() { // Circle의 넓이를 구하는 함수
return PI * r * r;
}
// 배열들의 합을 계산하기
public static double getArrArea(Circle[] circleArr) {
double sumArea = 0;
for (int i = 0; i < circleArr.length; i++) {
sumArea += circleArr[i].getArea();
// 배열에 선언되어있는 radius를 따와서 넓이를 구하고 sumArea에 더해서 저장
}
return sumArea;
}
}
class Rectangle {
private double width, height;
Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
public double getArea() {
return width * height;
}
public static double getArrArea(Rectangle[] recArr) {
double sumArea = 0;
for (int i = 0; i < recArr.length; i++) {
sumArea += recArr[i].getArea();
}
return sumArea;
}
}
public class JavaPractice {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Rectangle[] recArr = new Rectangle[2];
for (int i = 0; i < recArr.length; i++) {
System.out.println("가로 길이 : ");
double width = sc.nextDouble();
System.out.println("세로 길이 : ");
double height = sc.nextDouble();
recArr[i] = new Rectangle(width, height);
}
double areas2 = Rectangle.getArrArea(recArr);
System.out.println(areas2);
}
}
public class JavaPractice {
public static void main(String[] args) {
String[] strArr = new String[3];
strArr[0] = "abcdef";
strArr[1] = "abcdef";
strArr[2] = "abcdef";
System.out.println(getStrArr(strArr));
}
static int getStrArr(String[] strArr) {
int count = 0;
for (int i = 0; i < strArr.length; i++) {
count += strArr[i].length();
}
return count;
}
}
3-4에 이어서
import java.util.Scanner;
public class JavaPractice {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] strArr = new String[3];
for(int i=0; i<strArr.length; i++) {
System.out.println((i+1) + "번째 문자열 : ");
strArr[i] = sc.next();
}
System.out.println("문자열의 합 : " + getStrArr(strArr));
}
static int getStrArr(String[] strArr) {
int count = 0;
for (int i = 0; i < strArr.length; i++) {
count += strArr[i].length();
}
return count;
}
}