JavaStudy day5
주제
041 1부터 100까지 합 구하기
메서드를 선언하고 호출하는 방법을 확인한다.
- ScannerInput.java
import java.util.Scanner;
public class ScannerInput {
final static String help1 =
“Please Input int type One number:”;
public static int readInt(){
System.out.print(help1);
Scanner input = new Scanner(System.in);
return input.nextInt();
}
}
- ForOneToHundred41.java
package kr.co.infopub.chap041;
public class ForOneToHundred41 {
public static void main(String[] args) {
int toNum=100;
try{
toNum=ScannerInput.readInt();
}catch(Exception e){
System.out.println("예외: 타입확인 요망"+e);
System.exit(1);//프로그램 끝
}
//main
int summs=0;
for(int i=1;i<=toNum;i++){
summs+=i;
}
System.out.println("1 to "+toNum+" summation="+summs);
//메서드 1
int sum=summation(1,toNum);
System.out.println("1 to "+toNum+" summation="+sum);
//메서드 2
int sum2=summation(toNum);
System.out.println("1 to "+toNum+" summation="+sum2);
}
public static int summation(int start, int end){
if(start>end ){return -1;}
int sum=0;
for(int i=start;i<=end;i++){
sum+=i;
}
return sum;
}
public static int summation(int end){
return summation(1,end);
}
}
042 1부터 100까지 홀수 합, 짝수 합 구하기
- 메서드를 선언하고 호출하는 방법을 확인한다.
- ForOddEvenSum42.java
package kr.co.infopub.chap042;
public class ForOddEvenSum42 {
public static void main(String[] args) {
int toNum=100;
try{
toNum=ScannerInput.readInt();
}catch(Exception e){
System.out.println("예외: 타입확인 요망"+e);
System.exit(1);//프로그램 끝
}
int sumOdd=0;
int sumEven=0;
sumEven=summEven(1,toNum);
System.out.println("1 to "+toNum+" even sum "+sumEven);
sumOdd=summOdd(1,toNum);
System.out.println("1 to "+toNum+" odd sum "+sumOdd);
}
public static int summEven(int start, int end){
int sumEven=0;
//짝수합
for( int i=1; i<=end;i++){
if(i%2!=0){
continue;
}
sumEven+=i;
}
return sumEven;
}
public static int summOdd(int start, int end){
int sumOdd=0;
//짝수합
for( int i=1; i<=end;i++){
if(i%2==0){
continue;
}
sumOdd+=i;
}
return sumOdd;
}
}
043 규칙이 있는 수의 홀수 합, 짝수 합 구하기
- 등차수열, 등비수열, 점화식을 이해한다.
- NumberAn43.java
package kr.co.infopub.chap043;
public class NumberAn43 {
public static void main(String[] args) {
int toNum=10;
try{
toNum=ScannerInput.readInt();
}catch(Exception e){
System.out.println("예외: 타입확인 요망"+e);
System.exit(1);//프로그램 끝
}
int sum=sumAn(toNum,1,2); //등차수열
System.out.println("sum = "+sum);
int sum2=sumBn(toNum,1,2); //등비수열
System.out.println("sum = "+sum2);
int sum3=sumCn(toNum,2,3,-2);//점화식
System.out.println("sum = "+sum3);
}
public static int numAn(int start, int n, int d){
return (start+n*d);
}
public static int sumAn(int n,int start, int d){
int sum=0;
for( int i=0; i<n;i++){
System.out.print("["+numAn(start,i,d)+"] ");
sum+=numAn(start,i,d);
}
return sum;
}
public static int numBn(int start, int n, int r){
int nums=1;
for( int i=0; i<n;i++){
nums*=start*r;
}
return nums;
}
public static int sumBn(int n,int start, int r){
int sum=0;
for( int i=0; i<n;i++){
System.out.print("["+numBn(start,i,r)+"] ");
sum+=numBn(start,i,r);
}
return sum;
}
public static int numCn(int n,int start,int a, int d){
//An+1 = a*An+d 점화식
for( int i=0; i<n;i++){
start=a*start+d;
}
return start;
}
public static int sumCn(int n,int start, int a, int d){
int sum=0;
for( int i=0; i<n;i++){
System.out.print("["+numCn(i,start,a,d)+"] ");
sum+=numCn(i,start,a,d);
}
return sum;
}
}
044 규칙이 있는 수의 합 구하기
일정한 규칙을 찾는 연습을 한다.
- NumberAn44.java
package kr.co.infopub.chap044;
import kr.co.infopub.chap043.ScannerInput;
public class NumberAn44 {
public static void main(String[] args) {
int toNum=10;
try{
toNum=ScannerInput.readInt();
}catch(Exception e){
System.out.println("예외: 타입확인 요망"+e);
System.exit(1);//프로그램 끝
}
int sum=sumAn(toNum,1,2,true);
System.out.println("sum = "+sum);
int sum2=sumAn(toNum,1,2,false);
System.out.println("sum = "+sum2);
}
public static int numAn(int start, int n, int d){
return (start+n*d);
}
public static int posiNega(int start, int n, int d){
int oper=(n%2)==0?-1:1;
return oper*numAn(start,n,d);
}
public static int posiNega(int start, int n, int d, boolean isEvenNega){
int oper=isEvenNega?1:-1;
return oper*posiNega(start,n,d);
}
public static int sumAn(int n,int start, int d,boolean isEvenNega){
int sum=0;
for( int i=0; i<n;i++){
System.out.print("["+posiNega(start,i,d,isEvenNega)+"] ");
sum+=posiNega(start,i,d,isEvenNega);
}
return sum;
}
}
045 규칙이 있는 수의 합 구하기 2
일정한 규칙을 찾는 연습을 한다.
- NumberAn45.java
package kr.co.infopub.chap045;
public class NumberAn45 {
public static void main(String[] args) {
int toNum=10;
try{
toNum=ScannerInput.readInt();
}catch(Exception e){
System.out.println("예외: 타입확인 요망");
//System.exit(1);//프로그램 끝
}
double sum=sumAn(1,toNum,1);
System.out.println("\n1 to "+toNum+" summation="+sum);
double sum2=sumAn(1,toNum,2);
System.out.println("\n1 to "+toNum+" 1/odd summation="+sum2);
}
public static int numAn(int start, int n, int d){
return (start+n*d);
}
public static double divNum(int start, int n, int d){
return 1.0/numAn(start, n, d);
}
public static double sumAn(int start,int n, int d){
double sum=0;
for( int i=0; i<n;i++){
System.out.print("["+divNum(start,i,d)+"] ");
sum+=divNum(start,i,d);
}
return sum;
}
}
046 규칙이 있는 수의 합 구하기 3
일정한 규칙을 찾는 연습을 한다.
- NumberAn46.java
package kr.co.infopub.chap046;
public class NumberAn46 {
public static void main(String[] args) {
int toNum=10;//50이하입력
try{
toNum=ScannerInput.readInt();
}catch(Exception e){
System.out.println("예외: 타입확인 요망");
System.exit(1);//프로그램 끝
}
double sum=sumAn(1,toNum,1);
System.out.println("\n1 to "+toNum+" :="+sum);
double sum2=sumAn(1,toNum,2);
System.out.println("\n1 to "+toNum+" :="+sum2);
}
public static int numAn(int start, int n, int d){
return (start+n*d);
}
public static double divNum(int n){
return 1.0/fact(n);
}
public static double fact(int m){
double temp=1;
if(m<=0){
temp=1;
}
for(int i=m;i>0;i--){
temp*=i;
}
return temp;
}
public static double sumAn(int start,int n, int d){
double sum=0;
for( int i=0; i<n;i++){
System.out.print("["+divNum(numAn(start,i,d))+"] ");
sum+=divNum(numAn(start,i,d));
}
return sum;
}
}
047 사각형 안의 Hello 만들기
char와 int 간의 캐스팅을 이해하고, for문의 사용 방법을 익힌다.
- RectHellos47.java
package kr.co.infopub.chap047;
public class RectHellos47 {
public static void main(String[] args) {
printRectString("Hello",false);//영어는 공백 홀수개포함
printRectString("GO JAVA7",false);//영어는 공백 홀수개포함
printRectString("Hello my name is honny",false);
printRectString("반갑습니다",true);
System.out.println("Hello".length());
System.out.println("반갑습니다".length());
System.out.println();
System.out.println((int)'┐');//9488
System.out.println((int)'─');//9472
System.out.println((int)'│');//9474
System.out.println((int)'┌');//9484
System.out.println((int)'┘');//9496
System.out.println((int)'└');//9492
}
public static void printRectString(String str,boolean isE){
int count=str.length(); //String의 길이
//System.out.println(count);
int width=count;
if(!isE){
width=count+2;
}else{
width=count%2==0?count+3:count+4;
}
//┌────────────────────────┐ 9484 9472 9488
//System.out.print('\1'); //┌ 1 java2
System.out.print((char)9484); //┌ 9484 java7
for(int i=0;i<width;i++){
System.out.print((char)9472);//─ java7
//System.out.print('\6');//─ java2
}
//System.out.println('\2');//┐ java2
System.out.println((char)9488);//┐ 9488
//│ │ 9474
//System.out.print('\5');//│ java2
System.out.print((char)9474);
if(!isE){
System.out.print(" "+str+" ");//str
}else{
System.out.print(" "+str+" ");//str
}
System.out.println((char)9474);//│ 9474
//System.out.println('\5');//│ 9474
//└─────────────────────┘9492 9472 9496
//System.out.print('\3'); //└ java 2 3
System.out.print((char)9492);//└ 9492
for(int i=0;i<width;i++){
//System.out.print('\6');//─ 6
System.out.print((char)9472);//─ 9472
}
//System.out.println('\4');//┘ 4
System.out.println((char)9496);//┘ 9496
}
}
048 구구단 작성
중첩 for 문의 사용 방법을 익힌다. 멤버 메서드와 static 메서드의 사용상 차이점을 이해한다.
- GuGuDan.java
package kr.co.infopub.chap048;
public class GuGuDan48 {
public static void main(String[] args) {
//using method
printGuGu();
System.out.println("-----------------------------");
GuGu gu=new GuGu();
gu.printGu();
System.out.println("-----------------------------");
GuGu.printGuGu();
}
public static void printGuGu(){
for(int i=1; i<=9;i++){
for(int j=1; j<=9;j++){
System.out.printf("%d*%d=%d\t",j,i,i*j);
}
System.out.println();
}
}
}
class GuGu
{
public void printGu(){
for(int i=1; i<=9;i++){
for(int j=1; j<=9;j++){
System.out.printf("%d*%d=%d\t",j,i,i*j);
}
System.out.println();
}
}
public static void printGuGu(){
for(int i=1; i<=9;i++){
for(int j=1; j<=9;j++){
System.out.printf("%d*%d=%d\t",j,i,i*j);
}
System.out.println();
}
}
}
049 주사기 던지기
멤버 메서드와 static 메서드의 사용상 차이점을 이해한다.
- Die49.java
package kr.co.infopub.chap049;
public class Die49 {
public static void main(String[] args) {
Die.printAddDie();
Die.printMultiDie();
//non-static
Die die=new Die();
die.printnAddDie();
die.printnMultiDie();
}
}
class Die
{
public static void printAddDie(){
for(int i=1; i<=6;i++){
for(int j=1; j<=6;j++){
System.out.printf("%d+%d=%d\t",i,j,i+j);
}
System.out.println();
}
System.out.println("-----------------------------");
}
public void printnAddDie(){
for(int i=1; i<=6;i++){
for(int j=1; j<=6;j++){
System.out.printf("%d+%d=%d\t",i,j,i+j);
}
System.out.println();
}
System.out.println("-----------------------------");
}
public static void printMultiDie(){
for(int i=1; i<=6;i++){
for(int j=1; j<=6;j++){
System.out.printf("%d*%d=%d\t",i,j,i*j);
}
System.out.println();
}
System.out.println("-----------------------------");
}
public void printnMultiDie(){
for(int i=1; i<=6;i++){
for(int j=1; j<=6;j++){
System.out.printf("%d*%d=%d\t",i,j,i*j);
}
System.out.println();
}
System.out.println("-----------------------------");
}
}
050 여러 가지 모양 출력하기
규칙을 이끌어내는 방법을 익힌다.
- StarPoint50.java
package kr.co.infopub.chap050;
public class StarPoint50 {
public static void main(String[] args) {
printButterfly(5);
System.out.println("------------------");
printDiamond(5);
}
public static void printStarDecrease(int n){
for(int i=0; i<n;i++){
for(int j=0; j<i;j++){
System.out.print(" ");
}
for(int k=1; k<2*(n-i);k++){
System.out.print("*");
}
System.out.println();
}
}
public static void printStarIncrease(int n){
for(int i=n-1; i>=0;i--){
for(int j=0; j<i;j++){
System.out.print(" ");
}
for(int k=1; k<2*(n-i);k++){
System.out.print("*");
}
System.out.println();
}
}
public static void printButterfly(int n){
printStarDecrease(n);
printStarIncrease(n);
}
public static void printDiamond(int n){
printStarIncrease(n);
printStarDecrease(n);
}
}