티스토리 뷰
Write a Java program to answer about the statistical information such as arithmetic mean, median, mode, and standard deviation of an integer data set. (The arithmetic median, median, and mode mean that the arithmetic average, mid-point of the distribution, and most frequent response, respectively.) The input data can be generated by random method or keyboard. Your program should display the output similar to the one shown below.
package assignment;
import java.util.*;
public class Assignment {
public static void main(String[] args) {
var scanner = new Scanner(System.in);
System.out.print("Enter number of data points: ");
int length = scanner.nextInt(); //get length from user.
int[] arr = new int[length]; //initialize array
for(int i=0; i<length; i++) { //loop by length
System.out.print("int[" + i + "]: ");
arr[i] = scanner.nextInt(); //get data from user.
}
BubbleSort(length, arr); //sorting
System.out.print("Statistical Information: \n"
+ "------------------------------------ \n");
System.out.format("Arithmetic mean: %.6f\n", mean(arr));
System.out.format("Median: %.1f\n", median(arr));
System.out.format("Mode: %d\n", mode(arr));
System.out.format("Standard Deviation: %.6f\n", deviation(arr));
}
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;
}
}
}
}
public static double mean(int[] array) {
double sum = 0.0;
for (int i = 0; i < array.length; i++) {
sum += array[i];
}
return sum / array.length;
}
public static double median(int[] array) {
int middle = array.length/2;
if (array.length%2 == 1) {
return array[middle];
}
else {
return (array[middle-1] + array[middle]) / 2.0;
}
}
public static int mode(int[] array) {
int max_Value = 0;
int max_Count = 0;
for (int i = 0; i < array.length; ++i) {
int count = 0;
for (int j = 0; j < array.length; ++j) {
if (array[j] == array[i]) ++count;
}
if (count > max_Count) {
max_Count = count;
max_Value = array[i];
}
}
return max_Value;
}
public static double deviation(int[] array) {
double sum = 0.0, standardDeviation = 0.0;
int length = array.length;
for(double num : array) {
sum += num;
}
double mean = sum/length;
for(double num: array) {
standardDeviation += Math.pow(num - mean, 2);
}
return Math.sqrt(standardDeviation/length);
}
}
Write a program that adds the arguments and return the result as follows. You should use the concept of overloading in your program.
add(1, 2) --> 3
add(1, 2, 3) --> 6
add(1.4, 4.5) -->5.9
package assignment;
import java.util.*;
public class Assignment {
public static void main(String[] args) {
System.out.println("add(1,2) ==> " + add(1,2));
System.out.println("add(1,2,3) ==> " + add(1,2,3));
System.out.println("add(1.4,4.5) ==> " + add(1.4,4.5));
}
public static int add(int a, int b) { //add function for two integers
return a+b;
}
public static int add(int a, int b, int c) { //add function for three integers
return a+b+c;
}
public static double add(double a, double b) { //add function for two doubles
return a+b;
}
}
Write a program that will read a sequence of positive real numbers entered by the user and will print the same numbers in sorted order from smallest to largest. The user will input a zero to mark the end of the input. Assume that at most 100 positive numbers will be entered.
package assignment;
import java.util.*;
public class Assignment {
public static void main(String[] args) {
var scanner = new Scanner(System.in);
double data = 1;
double[] array = new double[100];
int n = 0;
while(data != 0 && n<100) { //most 100 positive numbers.
System.out.print("Input real number: ");
data = scanner.nextDouble();
if (data < 0) {
System.out.println("data didn't save. must be positve number.");
continue;
}
array[n] = data;
n++;
//sorting
for(int i=0; i<n-1; i++) {
for(int j=n-1; j>i; j--) {
if (array[j-1] > array[j]) {
double temp = array[j-1];
array[j-1] = array[j];
array[j] = temp;
}
}
} //sorting end.
for(int i=0; i<n; i++) { //after sorting print array.
System.out.print(array[i] + ", ");
}
System.out.print("\n");
}
System.out.println("program exit!");
}
}
Design a class hierarchy that simulates the things you have. For example, you have many electric devices such as laptop computer, smartphone, and mp3 player, many clothes such as pants, skirts, and jumper, etc. Your design should include abstract classes. Write a program that prints class name, instance variables, and methods of all classes. (Note: Draw your class hierarchy in your document.)
아래의 클래스구조와 같이 hierarchy를 설계함.
Device.java
package device;
public abstract class Device {
public abstract void hardware();
public abstract void software();
public void turnOn() {
System.out.println("Turn on the device.! Hi!!");
}
public void turnOff() {
System.out.println("Turn off the device.! Bye~");
}
public static void main(String[] args) {
Device macbook = new Macbook();
Device lte_controller = new LTE_controller();
Device externalmonitor = new ExternalMonitor();
macbook.hardware();
macbook.software();
macbook.turnOn();
macbook.turnOff();
System.out.print("\n");
lte_controller.hardware();
lte_controller.software();
lte_controller.turnOn();
lte_controller.turnOff();
System.out.print("\n");
externalmonitor.hardware();
externalmonitor.software();
externalmonitor.turnOn();
externalmonitor.turnOff();
}
}
ExternalMonitor.java
package device;
public class ExternalMonitor extends Device{
public void connect_Macbook() {
System.out.println("ExternalMonitor connect_Macbook()");
}
public void disconnect_Macbook() {
System.out.println("ExternalMonitor disconnect_Macbook()");
}
@Override
public void hardware() {
System.out.println("ExternalMonitor hardware() --> 27inch display");
}
@Override
public void software() {
System.out.println("ExternalMonitor software() --> realtime OS");
}
}
Ipad.java
package device;
public abstract class Ipad extends Device{
@Override
public void hardware() {
System.out.println("iPad hardware() --> A10X Fusion");
}
}
LTE_controller.java
package device;
public class LTE_controller extends Ipad {
@Override
public void software() {
System.out.println("LTE_controller software() --> ARM asm");
}
}
Macbook.java
package device;
public class Macbook extends Device {
@Override
public void hardware() {
System.out.println("Macbook hardware() --> intel i7");
}
@Override
public void software() {
System.out.println("Macbook software() --> Macintosh");
}
}
Write a program that uses the methods of ArrayList. This program prints 10 numbers randomly generated between 0 to 20. Your program should provide insert, remove, and search operations. Note that numbers should be sorted after all operations are finished. The following is an example.
List: 12456810111314 Input your command : a
Input number to add: 7
List: 124567810111314 Input your command : rInput number to add: 1
List: 2456810111314 Input your command : s Input number to search: 5 Index of 5 is 2.package assignment;
import java.util.*;
public class Assignment {
public static void main(String[] args) {
ArrayList<Integer> array = new ArrayList<Integer> ();
init_arr(array); //random 0~20 init array.
print_arr(array); //sorting & print lists.
var scanner = new Scanner(System.in);
while(true) {
System.out.print("Input your command: ");
String command = scanner.next();
int num = 0;
switch(command) {
case "a":
System.out.print("Input number to add: ");
num = scanner.nextInt(); //get number from user.
array.add(num); //add to array.
print_arr(array); //print arr.
break;
case "r":
System.out.print("Input number to remove: ");
num = scanner.nextInt();
if (array.contains(num)) { //if the number is exist.
array.remove(Integer.valueOf(num)); //remove from array.
print_arr(array); //print arr.
}
else {
System.out.println("Remove Error: " + num + " is not found."); //if not exist, error print
}
break;
case "s":
System.out.print("Input number to search: ");
num = scanner.nextInt(); //get number from user.
if (array.contains(num)) { //if the number is exist.
int index = array.indexOf(num); //save index of num
System.out.println("Index of " + num + " is " + index); //print result
}
else {
System.out.println("Search Error: " + num + " is not found."); //if not exist, error print
}
break;
default:
System.out.println("error: wrong command: (A)dd, (R)emove, (S)earch");
}
}
}
public static void init_arr(ArrayList<Integer> array) { //0~20 random number of 10 lists of array initialize
boolean same = false;
int random = (int)(Math.random()*21);
array.add(random);
while(true) { //random number 0~20 without duplicate number.
random = (int)(Math.random()*21); //initialize new random number.
for (int j=0; j<array.size(); j++) { //check duplicate number
if(random == array.get(j)) { //if same number found.
same = true;
break;
}
}
if(same == true) { //if same duplicated number found,
same = false;
continue; // initialize new random number on the top of loop
}
else {
array.add(random);
if(array.size() == 10) //array has 10 list, done.
break;
}
}
}
public static void print_arr(ArrayList<Integer> array) { //sorting & print lists function
Collections.sort(array); //array sorting using collections
System.out.print("List: ");
for (int i=0; i<array.size(); i++) { //print all lists.
System.out.print(array.get(i) + " ");
}
System.out.print("\n");
}
}
6. Boggle is a simple game that finds words sequentially linear-connected in a NxN grid. In the following example, you can find 2 words the "FLY", "MOON".
Design a light-weight boggle game that satisfies the following requirements.
1) Make a Boggle with a 3x3 grid.
2) Letters are randomly allocated.
3) User may input the word. Find this word at the grid. 4) Each word is at least three letters.
The following is an output example.
package assignment;
import java.util.*;
public class Assignment {
public static int xx;
public static int yy;
public static void main(String[] args) {
char [][] boggle = new char[5][5];
char[][] cp_array = new char[5][5];
init_boggle(boggle);
print_boggle(boggle);
while(true) {
System.out.print("Input word: ");
var scanner = new Scanner(System.in);
String input_word = scanner.next();
if (!word_length_checking(input_word)) {
System.out.println("Error: the word length should be 3~9 characters.");
}
else {
for(int i=0; i<5; i++) {
cp_array[i] = boggle[i].clone();
}
if (check_boogle(cp_array, input_word)) {
System.out.println("Exist!");
}
else {
System.out.println("Non-Exist!");
}
}
}
}
public static void init_boggle(char[][] array) {
final String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
final int N = alphabet.length();
Random r = new Random();
char random;
char word_array[] = new char[9];
boolean same = false;
int cnt = 0;
while(true) { //random number 0~20 without duplicate number.
random = alphabet.charAt(r.nextInt(N));; //initialize new random alphabet.
for (int j=0; j<word_array.length; j++) { //check duplicate number
if(random == word_array[j]) { //if same number found.
same = true;
break;
}
}
if(same == true) { //if same duplicated number found,
same = false;
continue; // initialize new random number on the top of loop
}
else {
if(cnt == 9) //array has 9 list, done.
break;
for(int i=0; i<9; i++) {
if (word_array[i] == 0) {
word_array[i] = random;
cnt ++;
break;
}
}
}
}
cnt = 0;
for(int i=1; i<4; i++) {
for(int j=1; j<4; j++) {
array[i][j] = word_array[cnt];
cnt++;
}
}
}
public static void print_boggle(char[][] array) {
for(int i=1; i<4; i++) {
System.out.print(" ------------- \n");
System.out.print(" | ");
for(int j=1; j<4; j++) {
System.out.print(array[i][j] + " | ");
}
System.out.print("\n");
}
System.out.print(" ------------- \n");
}
public static boolean check_boogle(char[][] arr, String word) {
String temp = word;
boolean success = false;
char a_char = word.charAt(0);
int i, j = 1;
boolean startingPointFound = false;
for(i=1; i<4; i++) {
for(j=1; j<4; j++) {
if(a_char == arr[i][j]) {
arr[i][j] = ' ';
temp = temp.substring(1);
startingPointFound = true;
break;
}
}
if (startingPointFound)
break;
}
if (startingPointFound) {
for(int k = 0; k<8; k++) {
if(near_char(arr, temp.charAt(0), i, j) == false) {
break;
}
else {
i = xx;
j = yy;
temp = temp.substring(1);
}
if(temp.length() == 0) {
success = true;
return true;
}
}
}
return false;
}
public static boolean near_char(char[][] array, char next ,int i, int j) {
for(int x = i-1; x<i+2; x++) {
for(int y = j-1; y<j+2; y++) {
//System.out.println("++ " + array[x][y]);
if(next == array[x][y]) {
//System.out.print( "near_char: \""+ next + "\" ");
array[x][y] = ' ';
xx = x;
yy = y;
return true;
}
}
}
return false;
}
public static boolean word_length_checking(String word) {
if(2 < word.length() && word.length() < 10) {
return true;
}
return false;
}
}
'알고리즘' 카테고리의 다른 글
Java 코드 정리 (Serialization, File I/O, Networking and Thread) (0) | 2018.12.06 |
---|---|
Java Programming 3 (0) | 2018.11.22 |
Java Programming 1 (0) | 2018.11.22 |
ethminer 맥북 충전기 유무 & 디스플레이 꺼짐 유무에 따라 채굴하기 (0) | 2018.11.22 |
윈도우 아파치 2.4 WebDAV 서버 설정 (1) | 2018.11.22 |
- Total
- Today
- Yesterday
- 슈어
- 우분투
- python
- 모바일 프로그래밍
- 알고리즘
- 코드업
- 외장그래픽
- eclipse
- 서버
- 환경오염
- NAS
- 자바
- 파이썬
- 개발
- 카카오톡
- CodeUp
- 문제
- 이클립스
- 프로그래밍
- Vega64
- 아키티오
- 산업화
- egpu
- Java
- 맥
- se846
- 백준
- 맥북
- 코딩
- C++
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |