알고리즘

Java Programming 1

이온시옥 2018. 11. 22. 01:19
반응형

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.

}

}





반응형