Java Programs that complete the following: Given A specific starting position an
ID: 3744592 • Letter: J
Question
Java Programs that complete the following:
Given A specific starting position and distance returns a 10x10 character array with all positions that are less than or equal to that manhattan distance from the starting position marked with an x Recall the manhattan distance is the distance in terms of number of direct steps North South East West.
a 2D array of characters is given and the function returns a string consisting of all the characters in column-order -- one column at a time, going from left to right.
Explanation / Answer
Program 1
import java.util.Scanner;
public class ManhattanDistance {
//marks the current position
static int currentX = 0;
static int currentY = 0;
static char[][] environment=null;
public static void main(String[] args)
{
getPosition();//gets current position
System.out.println("Enter distance:");
Scanner in = new Scanner(System.in);
int distance=in.nextInt();
getManhattanDistance(currentX,currentY,distance);
}
/**
* Gets current position from user as x and y
*/
private static void getPosition()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter current position:");
System.out.print("Row from top:");
currentX=in.nextInt()-1;
System.out.print("Column:");
currentY=in.nextInt()-1;
}
/**
* @param distance distance to mark
* @return 10X10 array
*/
private static char[][] getManhattanDistance(int x,int y,int distance)
{
ManhattanDistance d=new ManhattanDistance();//creates an object
int i,j;
int currentX=x;
int currentY=y;
environment[currentX][currentY] = 'P'; //marks current position
System.out.println("You are at:");
print();
//mark distance to west
for(i=currentY-1,j=0;i>=0&&j<distance;i--,j++)
environment[currentX][i] = 'X';
//mark distance to east
for(i=currentY+1,j=0;i<=9&&j<distance;i++,j++)
environment[currentX][i] = 'X';
//mark distance to north
for(i=currentX-1,j=0;i>=0&&j<distance;i--,j++)
environment[i][currentY] = 'X';
//mark distance to south
for(i=currentX+1,j=0;i<=9&&j<distance;i++,j++)
environment[i][currentY] = 'X';
print();
return environment;
}
ManhattanDistance()
{
environment = new char[10][10];
/**
* initialize the empty board
*/
for(int i=0; i<10; i++)
{
for(int j=0; j<10; j++)
{
environment[i][j] = '0';
}
}
}
//prints
public static void print()
{
System.out.println();
for(int i=0;i<=10;i++)
System.out.print((i)+" ");
System.out.println();
for(int i=0; i<10; i++)
{
for(int j=0; j<10; j++)
{
if(j==0)
System.out.print(i+1+" ");
System.out.print(environment[i][j] + " ");
}
System.out.println();
}
System.out.println();
}
sample output program1
Enter current position:
Row from top:5
Column:6
Enter distance:
3
You are at:
0 1 2 3 4 5 6 7 8 9 10
1 0 0 0 0 0 0 0 0 0 0
2 0 0 0 0 0 0 0 0 0 0
3 0 0 0 0 0 0 0 0 0 0
4 0 0 0 0 0 0 0 0 0 0
5 0 0 0 0 0 P 0 0 0 0
6 0 0 0 0 0 0 0 0 0 0
7 0 0 0 0 0 0 0 0 0 0
8 0 0 0 0 0 0 0 0 0 0
9 0 0 0 0 0 0 0 0 0 0
10 0 0 0 0 0 0 0 0 0 0
0 1 2 3 4 5 6 7 8 9 10
1 0 0 0 0 0 0 0 0 0 0
2 0 0 0 0 0 X 0 0 0 0
3 0 0 0 0 0 X 0 0 0 0
4 0 0 0 0 0 X 0 0 0 0
5 0 0 X X X P X X X 0
6 0 0 0 0 0 X 0 0 0 0
7 0 0 0 0 0 X 0 0 0 0
8 0 0 0 0 0 X 0 0 0 0
9 0 0 0 0 0 0 0 0 0 0
10 0 0 0 0 0 0 0 0 0 0
Program2
public class ColumnOrder {
ColumnOrder() {
}
public static void main(String[] args) {
char input[][]={{'a','b','c','d'},{'e','f','g','h'},{'i','j','k','m'},{'n','o','p','q'},{'r','s','t','u'}};//sample input
ColumnOrder c=new ColumnOrder();
c.printColumnWise(input);
}
/**
* Prints the inputted array column wise
* @param input 2D character array
* @return string array containing column wise input
*/
private String[] printColumnWise(char[][] input) {
int rows = input.length; //rows inputted
int cols = input[0].length; //columns inputted
String[] output=new String[cols];
String s="";
int pos=0;
System.out.println("2D character array:");
//prints inputted array
for(int i=0;i<rows;i++)
{
s="";
for(int j=0;j<cols;j++)
{
s=s+input[i][j];
}
System.out.println(s);
}
System.out.println("");
//prints column wise
for(int j=0;j<cols;j++)//iterating column wise
{
output[pos]="";
for(int i=0;i<rows;i++)
output[pos]=output[pos]+input[i][j];
pos++;
}
System.out.println("Column wise output:");
for(int k=0;k<cols;k++)
System.out.println(output[k]);
return output;
}
}
sample output program2
2D character array:
abcd
efgh
ijkm
nopq
rstu
Column wise output:
aeinr
bfjos
cgkpt
dhmqu
BUILD SUCCESSFUL (total time: 0 seconds)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.