Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Exercise_08/Exercise_08_11/Exercise_08_11.class
Binary file not shown.
29 changes: 29 additions & 0 deletions Exercise_08/Exercise_08_11/Exercise_08_11.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import java.util.Scanner;
public class Exercise_08_11{
public static void main(String[] args){
char[][] output = new char[3][3];
Scanner reader = new Scanner(System.in);
System.out.println("Enter a number between 0 and 511");
output = convert(reader.nextInt());
for(int i = 0; i < output.length; i++)
System.out.println(output[i][0] + " " + output[i][1] + " " + output[i][2]);

}
public static char[][] convert(int num){
char temp[][] = new char[3][3];
for(int i = 0,bit = 256; i < temp.length; i++){
for(int j = 0; j < temp[i].length; j++){
if(num % bit != num){
temp[i][j] = 'T';
num -= bit;
}
else{

temp[i][j] = 'H';
}
bit /= 2;
}
}
return temp;
}
}
Binary file added Exercise_08/Exercise_08_21/Exercise_08_21.class
Binary file not shown.
37 changes: 37 additions & 0 deletions Exercise_08/Exercise_08_21/Exercise_08_21.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import java.util.Scanner;
public class Exercise_08_21{

public static void main(String[] args){
Scanner reader = new Scanner(System.in);
System.out.print("Enter number of cities: ");
double[][] points = new double[reader.nextInt()][2];
for(int i = 0; i < points.length; i++){
points[i][0] = reader.nextDouble();
points[i][1] = reader.nextDouble();
}
double shortestDist = totalDistance(points,0);
int shortestPoint = 0;
double temp;
for(int i = 1; i < points.length; i++){
temp = totalDistance(points,i);
if(shortestDist > temp){
shortestDist = temp;
shortestPoint = i;
}
}
System.out.println("The central city is at(" + points[shortestPoint][0] + ", " + points[shortestPoint][1]+")");
System.out.println("The total distance to all other cities is " + shortestDist);

}
public static double totalDistance(double[][] array,int point){
double distance = 0;
for(int i = 0; i < array.length; i++)
distance += distance(array[point][0],array[point][1],array[i][0],array[i][1]);
return distance;
}


public static double distance(double x1, double y1, double x2, double y2){
return Math.sqrt(Math.pow(x2- x1, 2) + Math.pow(y2-y1,2));
}
}