티스토리 뷰
1. Write a program that calculates the area of circle using it’s radius. The following is an example.
Please enter the radius of a circle: 19 Area of a circle is 1134.114947
import java.util.*;
public class Main {
public static void main (String[] args) {
var scanner = new Scanner(System.in);
System.out.print("Please enter the radius of circle: ");
int radius = scanner.nextInt(); //get radius from user.
double result = radius * radius * 3.14159; //calculate area of circle
System.out.print("Area of a circle is " + result); //print out result
}
}
2. Write a JAVA program that orders an array of integer in descending order using bubble sort algorithm. You can refer the followings as an example of output.
Array Before Bubble Sort 5 90 35 45 150 3
Array After Bubble Sort
150 90 45 35 5 3
import java.util.*;
public class Main {
public static void main (String[] args) {
int[] arr = {5, 90, 35, 45, 150, 3}; //raw data.
//before sorting
System.out.println("Array Before Bubble Sort");
for (int i =0; i<arr.length; i++) { //array list print
System.out.print( arr[i] + " ");
}
System.out.print("\n");
BubbleSort(arr.length, arr); //sorting function.
//after sorting
System.out.println("Array After Bubble Sort");
for (int i =0; i<arr.length; i++) { //array list print
System.out.print( arr[i] + " ");
}
}
public static void BubbleSort(int length, int[] array) { //get length and array
int temp = 0; //for swap
for (int i=0; i<length-1; i++) { //descending sorting
for (int j= i+1; j<length; j++) {
if (array[i] < array[j]) {
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
}
}
3. Write a JAVA program that generates Pyramid for given number example. Some output examples are given as follows.
Enter Number: 5
Enter X:
1
0
12
345 6789
10 11 12 13 14
Enter Number: 5
Enter X:
2
0
24
6 8 10
12 14 16 18
20 22 24 26 28
Enter Number: 5
Enter X:
3
0
36
9 12 15
18 21 24 27
30 33 36 39 42
import java.util.*;
public class Main {
public static void main (String[] args) {
var scan = new Scanner(System.in);
System.out.print("Enter Number: ");
int num = scan.nextInt(); //get num from user.
System.out.print("Enter X: ");
int x = scan.nextInt(); //get x from user.
Pyamid(num, x); //pyamid function
}
public static void Pyamid(int number, int x) { //generate pyamid by number & x
int s_num = 0;
for (int j=0; j<number+1; j++) {
for (int i=0; i<j; i++) {
System.out.print(+ s_num);
Arrange(s_num); //arrange spaces by number length 0~9, 10~99, 100~999.
s_num += x; //add X
}
System.out.print("\n");
}
}
public static void Arrange(int number) { //arrange space by numbers
if (number < 10) {
System.out.print(" ");
}
else if (number < 100) {
System.out.print(" ");
}
else if (number < 1000) {
System.out.print(" ");
}
}
}
4. Write a program that simulates rolling a pair of dice. You can simulate rolling one die by choosing one of the integers 1, 2, 3, 4, 5, or 6 at random. The number you pick represents the number on the die after it is rolled. The expression
(int)(Math.random()*6) + 1
does the computation you need to select a random integer between 1 and 6. You can assign this value to a variable to represent one of the dice that are being rolled. Do this twice and add the results together to get the total roll. Your program should report the number showing on each die as well as the total roll. For example:
The first die comes up 3 The second die comes up 5 Your total roll is 8
import java.util.*;
public class Main {
public static void main (String[] args) {
RollingDice(); //function that simulate rolling two dice.
}
public static void RollingDice() {
int first = (int)(Math.random()*6)+1; //first dice random number generation
System.out.println("The first die comes up " + first); //display first
int second = (int)(Math.random()*6)+1; //second dice random number generation
System.out.println("The second die comes up " + second); //display second
System.out.println("Your total roll is " + (first+second)); //display total
}
}
5. Given three integers, a b c, write a class that returns true if two or more of them have the same rightmost digit. (The integers are non- negative.) Write a test code that runs your class, and verify it.
lastDigit(23, 19, 13) → true lastDigit(23, 19, 12) → false lastDigit(23, 19, 3) → true
import java.util.*;
public class Main {
public static void main (String[] args) {
boolean result = lastDigit(23, 19, 13); //compare last digit by lastDigit function.
System.out.println("lastDigit(23, 19, 13) --> " + result); //print result.
result = lastDigit(23, 19, 12); //compare last digit by lastDigit function.
System.out.println("lastDigit(23, 19, 12) --> " + result); //print result.
result = lastDigit(23, 19, 3); //compare last digit by lastDigit function.
System.out.println("lastDigit(23, 19, 3) --> " + result); //print result.
}
public static boolean lastDigit(int a, int b, int c) { //compare three numbers of lastDigit
//export each variables last digit
int a_last = a%10;
int b_last = b%10;
int c_last = c%10;
if(a_last == b_last || b_last == c_last || c_last == a_last) { //compare last digits
return true;
}
return false;
}
}
6. Write a program that asks the user's name, and then greets the user by name. Before outputting the user's name, convert it to upper case letters.
For example, if the user's name is Fred, then the program should respond "Hello, FRED, nice to meet you!".
import java.util.*;
public class Main {
public static void main (String[] args) {
var scan = new Scanner(System.in); //initialize scan instance.
System.out.print("What's your name?: ");
String name = scan.nextLine(); //get name from user.
System.out.println("Hello, " + name.toUpperCase() + ", nice to meet you!"); //change name to Uppercase and print greets.
}
}
'알고리즘' 카테고리의 다른 글
Java Programming 3 (0) | 2018.11.22 |
---|---|
Java Programming 2 (0) | 2018.11.22 |
ethminer 맥북 충전기 유무 & 디스플레이 꺼짐 유무에 따라 채굴하기 (0) | 2018.11.22 |
윈도우 아파치 2.4 WebDAV 서버 설정 (1) | 2018.11.22 |
Java coding, Boggle coding (0) | 2018.10.17 |
- Total
- Today
- Yesterday
- 코드업
- 환경오염
- 백준
- 서버
- C++
- 자바
- CodeUp
- 개발
- 프로그래밍
- egpu
- 슈어
- 알고리즘
- 아키티오
- se846
- 산업화
- Vega64
- python
- 파이썬
- 코딩
- 카카오톡
- NAS
- Java
- 문제
- 맥
- 모바일 프로그래밍
- 이클립스
- 맥북
- eclipse
- 외장그래픽
- 우분투
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |