반응형
Preface
메소드의 이름과 형식, 사용 방법만 알면 특별히 헷갈릴 만한 문제는 없다.
- 3번
package ch11;
import java.util.HashMap;
class Student {
private String studentNum;
public Student(String studentNum) {
this.studentNum = studentNum;
}
public String getStudentNum() {
return studentNum;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Student) {
Student student = (Student) obj;
if (studentNum.equals(student.studentNum)) {
return true;
}
}
return false;
}
@Override
public int hashCode() {
return studentNum.hashCode();
}
}
public class Exercise3 {
public static void main(String[] args) {
HashMap<Student, String> hashMap = new HashMap<Student, String>();
hashMap.put(new Student("1"), "95");
String score = hashMap.get(new Student("1"));
System.out.println("1번 학생의 총점: " + score);
}
}
- 4번
package ch11;
class Member3 {
private String id;
private String name;
public Member3(String id, String name) {
this.id = id;
this.name = name;
}
@Override
public String toString() {
return id + ": " + name;
}
}
public class Exercise4 {
public static void main(String[] args) {
Member3 member = new Member3("blue", "이파란");
System.out.println(member);
}
}
- 6번
package ch11;
public class Exercise6 {
public static void main(String[] args) {
byte[] bytes = { 73, 32, 108, 111, 118, 101, 32, 121, 111, 117 };
String str = new String(bytes);
System.out.println(str);
}
}
- 7번
package ch11;
public class Exercise7 {
public static void main(String[] args) {
String str = "모든 프로그램은 자바 언어로 개발될 수 있다.";
int index = str.indexOf("자바");
if(index == -1) {
System.out.println("자바 문자열이 포함되어 있지 않습니다.");
} else {
System.out.println("자바 문자열이 포함되어 있습니다.");
str = str.replace("자바", "Java");
System.out.println("->" + str);
}
}
}
- 8번
package ch11;
import java.util.StringTokenizer;
public class Exercise8 {
public static void main(String[] args) {
String str = "아이디,이름,패스워드";
String[] str1 = str.split(",");
for (int i = 0; i < str1.length; i++) {
System.out.println(str1[i]);
}
System.out.println();
StringTokenizer st = new StringTokenizer(str, ",");
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
}
}
- 9번
package ch11;
public class Exercise9 {
public static void main(String[] args) {
// String str = "";
// for(int i = 0; i <= 100; i++) {
// str += i;
// }
// System.out.println(str);
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= 100; i++) {
sb.append(i);
}
System.out.println(sb);
}
}
- 10번
package ch11;
import java.util.regex.Pattern;
public class Exercise10 {
public static void main(String[] args) {
String id = "5Angel1004";
String regExp = "[[a-zA-Z][a-zA-Z0-9] {7,11}"; // 개수 범위는 +1씩인가??
boolean isMatch = Pattern.matches(regExp, id);
if (isMatch) {
System.out.println("available");
} else {
System.out.println("not available");
}
}
}
- 12번
package ch11;
public class Exercise12 {
public static void main(String[] args) {
String strData1 = "200";
int intData1 = Integer.parseInt(strData1);
int intData2 = 150;
String strData2 = Integer.toString(intData2);
}
}
- 13번
package ch11;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Exercise13 {
public static void main(String[] args) {
Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy년 MM월 dd일 E요일 k시 m분");
String strDate = sdf.format(now);
System.out.println(strDate);
}
}
728x90
반응형
'Java > 이것이 자바다' 카테고리의 다른 글
이자바 12장(멀티 스레드) 확인문제 (0) | 2023.04.25 |
---|---|
멀티 스레드 (0) | 2023.04.21 |
기본 API 클래스 (3) (0) | 2023.04.15 |
기본 API 클래스 (2) (0) | 2023.04.14 |
기본 API 클래스 (1) (0) | 2023.04.13 |
댓글