일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
- 권한변경
- 로그아웃
- 국취제
- live server 환경설정
- forward
- 인코딩
- 별찍기
- redirect
- jdbc설정
- mvc
- 페이징
- 페이지 재사용
- 회원탈퇴
- jsp기본
- github
- emmet환경설정
- 회원정보수정
- 검색기능
- Git
- 내일배움카드
- 비밀번호암호화
- 비밀번호변경
- 배열
- 입력메소드
- 국민취업지원제도
- 관리자회원조회
- 정처기
- 국비학원
- 내배카
- jdbc환경설정
- Today
- Total
기록
*13일차 실습 본문
[문제 2] non-static
package com.oop.method.nonstatic;
public class NonStaticSample {
//1. 반환값 없고 매개변수 없는 메소드
//실행 요청시 1~45까지의 임의의 정수 6개 중복되지 않게 발생시켜 출력하는 메소드
//메소드명 : printLottoNumbers
//2. 반환값 없고 매개변수 있는 메소드
//실행 요청시 정수 하나, 문자 하나를 전달받아 문자를 정수 갯수만큼 출력하는 메소드
//메소드명 : outputChar
//3. 반환값 있고 매개변수 없는 메소드
//실행 요청시 알파벳 범위의 임의의 영문자를 하나 발생시켜 리턴하는 메소드
//메소드명 : alphabet
//4. 반환값 있고 매개변수 있는 메소드
//실행 요청시 문자열과 시작인덱스, 끝인덱스를 전달받아 해당 인덱스 범위의 문자열을
//추출하여 리턴하는 메소드. 단 문자열은 반드시 값이 있어야함. 없으면 null 리턴처리
//메소드명 : mySubstring
}
------------------------------------------------------------------------------------------------------------
package kh.java.oop.method.nonstatic;
import java.lang.*;
import java.util.Random;
import java.util.Scanner;
public class NonStaticSample {
Scanner sc = new Scanner(System.in);
public void printLottoNumbers() {
int arr[] = new int[6];
for (int i = 0; i < arr.length; i++) {
arr[i] = (int) (Math.random() * 45 + 1); // 1~45
for (int j = 0; j < i; j++) {
if (arr[i] == arr[j])
i--;
}
}
System.out.print("랜덤 수: ");
System.out.print("[");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println("]");
System.out.println();
}
public void outputChar(char ch, int n) {
System.out.print("정수 입력: ");
n = sc.nextInt();
System.out.print("문자 입력: ");
ch = sc.next().charAt(0);
System.out.print("[");
for (int i = 0; i < n; i++) {
System.out.print(ch + " ");
}
System.out.println("]");
System.out.println();
}
public char alphabet() {
char ch='0';
int num=(int)(Math.random()*2+1);
switch(num) {
case 1:
ch =(char)(Math.random() * ( (90-65)+1)+65); //대문자
System.out.print(ch);
break;
case 2:
ch =(char)(Math.random() * ( (122-97)+1)+97); //소문자
System.out.print(ch);
break;
}
System.out.println();
return ch;
}
public String mySubstring(String str, int a, int b) {
char arr[] = new char[str.length()];
for (int i = 0; i < str.length(); i++) {
arr[i] = str.charAt(i);
}
for (int i = 0; i < str.length(); i++) {
if ((i > a && i < b)) {// 시작인덱스~끝 인덱스
System.out.print(arr[i]);
} else if (i == a && i == b) {
System.out.println("null");
}
}
return str;
}
}
[문제 3] static
public class StaticMethod {
//static method
//1. 전달한 문자열을 모두 대문자로 바꾸는 static 메소드
//메소드명 : toUpper(String) : String
//2. 문자열(1)에서 전달받은 인덱스(2)의 문자를 전달받은 문자(3)로 변경하는 static 메소드
//메소드명 : setChar(String, int, char) : String
//3. 전달한 문자열에서 영문자의 개수를 리턴하는 static 메소드
//메소드명 : getAlphabetLength(String) : int
//4. 전달한 문자열값을 하나로 합쳐서 리턴
//메소드명 : concat(String, String) : String
}
------------------------------------------------------------------------------------------------------------
package kh.java.oop.method.static_;
public class StaticMethod {
public static String toUpper(String s) {
return s.toUpperCase();
}
public static String _setChar(String str, int i ,char ch) {
// char[]arr = new char[str.length()];
char[]arr = str.toCharArray();
arr[i] = ch;
return new String(arr);
}
public static String setChar(String str, int i , char ch) {
//index이전
//index가 2인경우 0~1읽어서 문자열로 반환
String before = str.substring(0,i);
System.out.println("before = "+before );
//index이후 문자열
String after = str.substring(i+1);
System.out.println("after = "+after );
return before + ch + after;
}
public static int getAlphabetLength(String s) {
return s.length();
}
public static String Concat(String s1,String s2) {
return s1+s2;
}
}