Making a hollow diamond with a word in it - java

What I need is a little modification to my code so that every part of my hollow diamond prints a letter of the word "HURRICANE"
My code is:
String st1 = "HURRICANE";
int a = 0;
for (int i = 5; i >= 1; i--) {
for (int j = 1; j <= 9; j++) {
if (j == i || (10 - i) == j) {
System.out.print(st1.charAt(a)); //needs change
} else {
System.out.print(' ');
}
}
System.out.println();
}
for (int i = 2; i <= 5; i++) {
for (int j = 1; j <= 9; j++) {
if (j == i || (10 - i) == j) {
System.out.print(st1.charAt(a)); //needs change
} else {
System.out.print(' ');
}
}
System.out.println();
}
The output comes out as:
H
H H
H H
H H
H H
H H
H H
H H
H
I need to modify my "charAt" statement a little so it comes out to be:
H
U U
R R
R R
I I
C C
A A
N N
E
How should I make my print statement?

It's worth noting that the example provided only works for Strings the same length as "HURRICANE". A superior solution would work for all strings.
Partial solution for you to complete, since I guess it's your coursework and I don't want you to copy / paste / fail exams :P
public static void main(String[] args) {
String st1 = "HURRICANE";
char[] st1CharArray = st1.toCharArray();
int maxSpaces = st1CharArray.length / 2 + 1;
for (int i = 0; i <= st1CharArray.length / 2; i++) {
if (i == 0) {
System.out.println(getSpacesString(maxSpaces) + st1CharArray[i]);
} else {
System.out.println(getSpacesString(maxSpaces - i)
+ st1CharArray[i] + getSpacesString(i * 2 - 1)
+ st1CharArray[i]);
}
}
// Loop from st1CharArray.length / 2 + 1 and get the second half done.
}
private static String getSpacesString(int numberOfSpaces) {
StringBuilder strBuilder = new StringBuilder();
for (int i = 0; i < numberOfSpaces; i++) {
strBuilder.append(" ");
}
return strBuilder.toString();
}

//: Playground - noun: a place where people can play
import UIKit
var name : String = "HURRICANE"
var dimensions : Int = name.count - 1
var k : Int = 0
for rows in 0...dimensions{
for columns in 0...dimensions{
k = abs( (dimensions/2) - rows )
if columns == k || columns == dimensions - k{
print(Array(name)[rows], terminator: "")
}
else{
print(" ", terminator: "" )
}
}
print("")
}

String st1 = "HURRICANE";
int a = 0;
for (int i = 5; i >= 1; i--) {
for (int j = 1; j <= 9; j++) {
if (j == i || (10 - i) == j) {
System.out.print(st1.charAt(5 - i));
} else {
System.out.print(' ');
}
}
System.out.println();
}
for (int i = 2; i <= 5; i++) {
for (int j = 1; j <= 9; j++) {
if (j == i || (10 - i) == j) {
System.out.print(st1.charAt(3 + i));
} else {
System.out.print(' ');
}
}
System.out.println();
}

Let's assume that a word has an odd number of characters, otherwise we get a crooked diamond.
Try it online!
public static void main(String[] args) {
String str = "abrahadabra";
int n = str.length() / 2;
for (int i = -n, ch = 0; i <= n && ch < str.length(); i++, ch++) {
for (int j = -n; j <= n; j++)
if (Math.abs(i) + Math.abs(j) == n)
System.out.print(str.charAt(ch));
else
System.out.print(" ");
System.out.println();
}
}
Output:
a
b b
r r
a a
h h
a a
d d
a a
b b
r r
a

Related

How can I test credit numbers with hyphens (" -" ) to get it as INVALID . When I tried 4003-6000-0000-0014 i am getting errors

I cant get credit card numbers containing hymens as INVALID such as 4003-6000-0000-0014 must give me INVALID but its giving me errors of string.
public class prog {
public static void main(String[] args)
{
Scanner userInput = new Scanner(System.in) ;
System.out.println("How many credit card you want to check?");
int numOfCredit = userInput.nextInt() ;
int creditnumbers[] = new int[numOfCredit] ;
for (int i = 0 ; i<numOfCredit ; i++)
{
System.out.println("Enter credit card number "+(i+1)+": ") ;
String creditNumber = userInput.next() ;
validateCreditCardNumber(creditNumber);
}
}
private static void validateCreditCardNumber(String str) { // function to check credit numbers
int[] ints = new int[str.length()];
for (int i = 0; i < str.length(); i++) {
ints[i] = Integer.parseInt(str.substring(i, i + 1));
}
for (int i = ints.length - 2; i >= 0; i = i - 2) {
int j = ints[i];
j = j * 2;
if (j > 9) {
j = j % 10 + 1;
}
ints[i] = j;
}
int sum = 0;
for (int i = 0; i < ints.length; i++)
{
sum += ints[i];
}
if (sum % 10 == 0)
{
System.out.println("VALID");
}
else
{
System.out.println("INVALID");
}
}
}
I get these errors after running with hymens :
Exception in thread "main" java.lang.NumberFormatException: For input string: "-"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68)
at java.base/java.lang.Integer.parseInt(Integer.java:642)
at java.base/java.lang.Integer.parseInt(Integer.java:770)
at testing/testing.prog.validateCreditCardNumber(prog.java:33)
at testing/testing.prog.main(prog.java:22)
you can replace in your string "-" with "" (blank) and then apply this function:
String card = "4003-6000-0000-0014";
Boolean t = check(card.replace("-",""));
public static boolean check(String ccNumber)
{
int sum = 0;
boolean alternate = false;
for (int i = ccNumber.length() - 1; i >= 0; i--)
{
int n = Integer.parseInt(ccNumber.substring(i, i + 1));
if (alternate)
{
n *= 2;
if (n > 9)
{
n = (n % 10) + 1;
}
}
sum += n;
alternate = !alternate;
}
return (sum % 10 == 0);
}

matlab bwmorph(img, 'thin') implementation in Java gone wrong

I am implementing the matlab 'bwmorph(img, 'thin')' algorithm in Java ImageJ. I've searched all over the net pretty much and found some similar implementations that work better, but I can't find the issue in my code. Any ideas?
My code:
public void run(ImageProcessor ip) {
MakeBinary(ip);
int sum2 = processThin(ip);
int sum = -1;
while (sum2 != sum) {
sum = sum2;
sum2 = processThin(ip);
}
}
public int processThin(ImageProcessor ipOriginal) {
int sum = 0;
// first iteration
ImageProcessor ip = ipOriginal.duplicate();
for (int i = 1; i < ip.getWidth() -1; i++)
for (int j = 1; j < ip.getHeight() -1; j++) {
int[] neighbors = selectNeighbors(ip, i, j);
if (G1(neighbors) == 1 && G2(neighbors) >= 2 && G2(neighbors) <= 3 && G3(neighbors) == 0)
ip.putPixel(i,j, 0);
}
// second iteration
for (int i = 1; i < ip.getWidth() -1; i++)
for (int j = 1; j < ip.getHeight()-1; j++) {
int[] neighbors = selectNeighbors(ip, i, j);
if (G1(neighbors) == 1 && G2(neighbors) >= 2 && G2(neighbors) <= 3 && G3prime(neighbors) == 0)
ip.putPixel(i,j, 0);
}
for(int i = 0; i < ip.getWidth(); i++)
for(int j = 0; j < ip.getHeight(); j++) {
if (ip.getPixel(i,j) != 0) sum++;
ipOriginal.putPixel(i, j, ip.getPixel(i, j));
}
return sum;
}
private int G1(int[] input) {
int xh = 0;
for (int i = 1; i <= 4; i++) {
if (input[2 * i - 1] == 0 && (input[2 * i] == 1 || (2 * i + 1 <= 8 ? input[2 * i + 1] == 1 : input[1] == 1)))
xh += 1;
}
return xh;
}
private int G2(int[] input) {
int n1 = 0, n2 = 0;
n1 = toInt(toBool(input[4]) || toBool(input[3])) + toInt(toBool(input[1]) || toBool(input[2])) +
toInt(toBool(input[8]) || toBool(input[7])) + toInt(toBool(input[6]) || toBool(input[5]));
n2 = toInt(toBool(input[2]) || toBool(input[3])) + toInt(toBool(input[1]) || toBool(input[8])) +
toInt(toBool(input[6]) || toBool(input[7])) + toInt(toBool(input[4]) || toBool(input[5]));
return Math.min(n1,n2);
}
private int G3 (int[] input){
return toInt((toBool(input[2]) || toBool(input[3]) || !toBool(input[8])) && toBool(input[1]));
}
private int G3prime (int[] input){
return toInt((toBool(input[6]) || toBool(input[7]) || !toBool(input[4])) && toBool(input[5]));
}
private boolean toBool(int i ){
return i == 1;
}
private int toInt(boolean i) {
return i ? 1 : 0;
}
private int[] selectNeighbors(ImageProcessor ip, int i, int j) {
int[] result = new int[9];
result[1] = ip.getPixel(i+1,j);
result[2] = ip.getPixel(i+1,j+1);
result[3] = ip.getPixel(i,j+1);
result[4] = ip.getPixel(i-1,j+1);
result[5] = ip.getPixel(i-1,j);
result[6] = ip.getPixel(i-1,j-1);
result[7] = ip.getPixel(i,j-1);
result[8] = ip.getPixel(i+1,j-1);
for (int x = 0; x < result.length; x++)
if (result[x] != 0) result[x] = 1;
return result;
}
The main issue appears to be with the horizontal lines, but not only that.
Note: I've added the toBool and toInt methods to deal with convenient data types, the code was binary before and the result is the same apparently.
EDIT:
After editing the code and omitting doing modifications between two iterations, I ended up with this result now.
The code looks like this now.
public int processThin(ImageProcessor ip) {
int sum = 0;
// first iteration
int[][] mask = new int[ip.getWidth()][ip.getHeight()];
for (int i = 1; i < ip.getWidth() -1; i++)
for (int j = 1; j < ip.getHeight() -1; j++) {
int[] neighbors = selectNeighbors(ip, i, j);
if (G1(neighbors) == 1 && G2(neighbors) >= 2 && G2(neighbors) <= 3 && G3(neighbors) == 0)
mask[i][j]++;
}
// second iteration
for (int i = 1; i < ip.getWidth() -1; i++)
for (int j = 1; j < ip.getHeight()-1; j++) {
int[] neighbors = selectNeighbors(ip, i, j);
if (G1(neighbors) == 1 && G2(neighbors) >= 2 && G2(neighbors) <= 3 && G3prime(neighbors) == 0)
mask[i][j]++;
}
for(int i = 0; i < ip.getWidth(); i++)
for(int j = 0; j < ip.getHeight(); j++) {
if (mask[i][j] != 0) sum++;
ip.putPixel(i, j, mask[i][j] > 0 ? 0 : ip.getPixel(i,j));
}
return sum;
}
The problem in your original code is that you write into your input image. In the very first iteration, moving left to right, you remove successive pixels because each has, after modifying the previous pixel, a background pixel as neighbor.
There are different ways to implement the thinning operation, but the simplest one that works in-place like your code does requires two passes through the image for each iteration of the thinning:
Go through the image and mark all candidate pixels. These are the pixels that have a background neighbor. Marking a pixel can be as simple as setting the pixel value to a given constant, for example 42 (assuming background is 0 and foreground is 1 or 255 or whatever you decided on).
Go through the image again and for each marked pixel, determine if removing it would change the geometry of the foreground. If not, remove it. In this test, take the marked pixels that haven't been removed yet as foreground.

Spiral diamond -print pattern

I'm currently try to print below pattern in java,please help me in solving it.
Input: 3, abcdefghijklm
output:
c
bjd
aimke
hlf
g
I build diamond pattern with star, stuck in print values from array in spirally in diamond shape show above.
public static void rhombus() {
int n = 3;
int size = 2 * n - 1;
char[][] sol = new char[size][size];
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
sol[i][j] = ' ';
}
}
int i = size / 2;
int j = 0;
String s = "abcdefghijklmnop";
int len = s.length();
int in = 0;
int left = 0, top = 0, right = size - 1;
int bottom = size - 1;
boolean flag = false;
while (i != j) {
while (i >= top && !flag) {
sol[i][j] = s.charAt((in++) % len);
if (i == size / 2 && j == size / 2)
flag = true;
i--;
j++;
}
if (flag == true)
break;
i += 2;
top++;
left++;
while (j <= right && !flag) {
sol[i][j] = s.charAt((in++) % len);
i++;
j++;
}
j -= 2;
right--;
while (i <= bottom && !flag) {
sol[i][j] = s.charAt((in++) % len);
i++;
j--;
}
bottom--;
i -= 2;
while (j >= left && !flag) {
sol[i][j] = s.charAt((in++) % len);
i--;
j--;
}
j++;
}
sol[i][j] = s.charAt((in++) % len);
for (int a = 0; a < size; a++) {
for (int b = 0; b < size; b++) {
System.out.print(sol[a][b]);
}
System.out.println();
}
}

I am beginner, why "error: '.class' expected" is appear? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
This is a program for calculate the area of triangle that create by six points
and point out what types of triangle.
import java.util.Scanner;
public class assignment {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int m = 0,n = 0;
int[][] x = new int[6][2];
double a,b,c,a1,b1,c1;
String y = "";
System.out.println("Please enter six points,");
for(int i = 0 ; i < x.length ; i++ ){
for(int j = 0 ; j < x[i].length ; j++)
x[i][j] = input.nextInt();
}
for(int i = 0 ; i < 30 ; i++)
System.out.print("--");
System.out.print("\n\t\t\t\tTypes of Triangles\n");
for(int i = 0 ; i < 30 ; i++)
System.out.print("--");
System.out.println();
System.out.println("Point1\t\tPoint2\t\tPoint3\t\tType of Triangle");
for(int i = 0 ; i < 30 ; i++)
System.out.print("--");
System.out.println();
for(int j = 0 ; j < 4 ; j++){
for(int k = j + 1 ; k < 5 ; k++ ){
for(int l = k + 1 ;l < 6 ; l++ ){
a = Math.sqrt(Math.pow((x[j][0]-x[k][0]),2)+Math.pow((x[j][1]-x[k][1]),2));
b = Math.sqrt(Math.pow((x[j][0]-x[l][0]),2)+Math.pow((x[j][1]-x[l][1]),2));
c = Math.sqrt(Math.pow((x[l][0]-x[k][0]),2)+Math.pow((x[l][1]-x[k][1]),2));
c1=Math.max(Math.max(a,b),c);
b1=Math.min(Math.min(a,b),c);
a1=(a+b+c)-c1-b1;
if ( a1 > 0 && b1 > 0 && c1 > 0 && a1 + b1 > c1 && b1 + c1 > a1 && c1 + a1 > b1 ){
if(Math.pow(c1,2) == (Math.pow(b1,2) + Math.pow(a1,2))){
if(a1==b1 || b1==c1 || c1==a1){
y = ("Right-angled\tIsosceles");
}
else if(a1!=b1 && b1!=c1 && c1!=a1){
y = ("Right-angled\tScalene");
}
}
else if(a1==b1 || b1==c1 || c1==a1){
y = ("Isosceles");
}
else if(a1!=b1 && b1!=c1 && c1!=a1){
y=("Scalene");
}
}
else{
y = ("Non-triangle");
}
System.out.println("(" + x[j][0] + "," + x[j][1]+ ")\t\t" + "(" + x[k][0] + "," + x[k][1] + ")\t\t" + "(" + x [l][0] + "," + x[l][1]+")\t\t" + y );
}
}
}
System.out.print("Maximum area of triangle =" + Math.max(area()));
System.out.print("Maximum area of triangle =" + Math.min(area()));
}
public static double area(double[][] n){
double s = 0;
int i;
for(int j = 0 ; j < 4 ; j++){
for(int k = j + 1 ; k < 5 ; k++ ){
for(int l = k + 1 ;l < 6 ; l++ ){
a = Math.sqrt(Math.pow((x[j][0]-x[k][0]),2)+Math.pow((x[j][1]-x[k][1]),2));
b = Math.sqrt(Math.pow((x[j][0]-x[l][0]),2)+Math.pow((x[j][1]-x[l][1]),2));
c = Math.sqrt(Math.pow((x[l][0]-x[k][0]),2)+Math.pow((x[l][1]-x[k][1]),2));
s = (a+b+c)/2;
n[i][0] = Math.sqrt(s * (s-a) * (s-b) * (s-c));
i++;
}
}
}
return n[][];
}
}
I have got a error: '.class' expected,
I am a beginner ,can someone help me please?
thx a lot
There are quite a few problems with the code you posted. Things like assigning to undefined variables such as the following in the area method:
a = Math.sqrt(Math.pow((x[j][0]-x[k][0]),2)+Math.pow((x[j][1]-x[k][1]),2));
You're also referencing the variable x in that statement which is not the correct variable name. The return type and value returned don't match, and there's a syntax problem in the return.
A lot of these problems will be pointed out using an IDE like Eclipse. I think you would do yourself a favor to learn coding in Eclipse.
Here is a revision of your code that runs. I'm not sure about correctness of what you're trying to do:
public class assignment {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int m = 0, n = 0;
int[][] x = new int[6][2];
double a, b, c, a1, b1, c1;
String y = "";
System.out.println("Please enter six points,");
for (int i = 0; i < x.length; i++) {
for (int j = 0; j < x[i].length; j++)
x[i][j] = input.nextInt();
}
for (int i = 0; i < 30; i++)
System.out.print("--");
System.out.print("\n\t\t\t\tTypes of Triangles\n");
for (int i = 0; i < 30; i++)
System.out.print("--");
System.out.println();
System.out.println("Point1\t\tPoint2\t\tPoint3\t\tType of Triangle");
for (int i = 0; i < 30; i++)
System.out.print("--");
System.out.println();
for (int j = 0; j < 4; j++) {
for (int k = j + 1; k < 5; k++) {
for (int l = k + 1; l < 6; l++) {
a = Math.sqrt(Math.pow((x[j][0] - x[k][0]), 2) + Math.pow((x[j][1] - x[k][1]), 2));
b = Math.sqrt(Math.pow((x[j][0] - x[l][0]), 2) + Math.pow((x[j][1] - x[l][1]), 2));
c = Math.sqrt(Math.pow((x[l][0] - x[k][0]), 2) + Math.pow((x[l][1] - x[k][1]), 2));
c1 = Math.max(Math.max(a, b), c);
b1 = Math.min(Math.min(a, b), c);
a1 = (a + b + c) - c1 - b1;
if (a1 > 0 && b1 > 0 && c1 > 0 && a1 + b1 > c1 && b1 + c1 > a1 && c1 + a1 > b1) {
if (Math.pow(c1, 2) == (Math.pow(b1, 2) + Math.pow(a1, 2))) {
if (a1 == b1 || b1 == c1 || c1 == a1) {
y = ("Right-angled\tIsosceles");
} else if (a1 != b1 && b1 != c1 && c1 != a1) {
y = ("Right-angled\tScalene");
}
} else if (a1 == b1 || b1 == c1 || c1 == a1) {
y = ("Isosceles");
} else if (a1 != b1 && b1 != c1 && c1 != a1) {
y = ("Scalene");
}
} else {
y = ("Non-triangle");
}
System.out.println("(" + x[j][0] + "," + x[j][1] + ")\t\t" + "(" + x[k][0] + "," + x[k][1] + ")\t\t" + "(" + x[l][0] + "," + x[l][1] + ")\t\t" + y);
}
}
}
System.out.print("Maximum area of triangle =" + area(x));
//System.out.print("Maximum area of triangle =" + Math.min(area(x)));
}
public static double area(int[][] n) {
double s = 0;
double max = 0;
for (int j = 0; j < 4; j++) {
for (int k = j + 1; k < 5; k++) {
for (int l = k + 1; l < 6; l++) {
double a = Math.sqrt(Math.pow((n[j][0] - n[k][0]), 2) + Math.pow((n[j][1] - n[k][1]), 2));
double b = Math.sqrt(Math.pow((n[j][0] - n[l][0]), 2) + Math.pow((n[j][1] - n[l][1]), 2));
double c = Math.sqrt(Math.pow((n[l][0] - n[k][0]), 2) + Math.pow((n[l][1] - n[k][1]), 2));
s = (a + b + c) / 2;
if (s > max) {
max = s;
}
}
}
}
return max;
}
}

Nested loop construction

This is part of my homework. All I need is a bit of advice. I need to write some nested loop constructs, to print the following:
"122333444455555"
"+**+++****+++++"
"--***++++-----******+++++++"
Here is my code to print the first set of symbols
public static void main (String[] args)
{
int i,j;
for(i=1;i<6;++i)
{
for(j=1;j<i+1;++j)
{
System.out.print(i);
}
}
}
This works perfectly fine. I'm just having trouble figuring out the second and third set of symbols. Apologies for my lack of experience, I'm fairly new to Java.
One solution is:
final String[] arr = {"*", "+"};
And in your inner loop:
System.out.print(arr[i % 2]);
The % (Modulo) operator is responsible of the switches between * and + symbols:
For even i it'll be *, otherwise it'll be +.
Output: "+**+++****+++++".
(Regarding the second output, I'll not show you the solution, but it's very similar to this one once you understand it).
public static void main(String[] args) throws IOException {
int i, j;
for (i = 1; i < 6; ++i) {
for (j = 1; j < i + 1; ++j) {
System.out.print(i);
}
}
System.out.println();
for (i = 1; i < 6; i++) {
if (i % 2 == 1) {
for (j = 1; j < i + 1; ++j){
System.out.print("+");
}
} else {
for (j = 1; j < i + 1; ++j){
System.out.print("*");
}
}
}
System.out.println();
for (i = 2; i < 8; i++) {
if (i % 3 == 1) {
for (j = 1; j <= i; ++j){
System.out.print("+");
}
} else if (i % 3 == 2) {
for (j = 1; j <= i; ++j){
System.out.print("-");
}
} else {
for (j = 1; j <= i; ++j){
System.out.print("*");
}
}
}
}
Cycle #1:
You have to print out numbers from one to five and each number N has to be printed out N times.
for (i = 1; i < 6; ++i) { // this would set `i` to numbers from 1-5
for (j = 1; j < i + 1; ++j) { // for each cycle (next number) it prints
//it out N times where N is the cycle number. 1 is the first cycle,
//2 is the second and so on.
Cycle #2:
Same problem but instead of printing out number of the cycle you have to print out + or * based on if the cycle number is odd or even.
To check if the number is even you can use:
int number = 1;
if(number % 2 == 0){ // is true if the number is even
This checks whats the remainder from the division of number by two.
Cycle #3:
Same as #2 but you start from the second cycle, not from the first and you check for the remainder after division by 3.
If I understand, the third set is composed by sequence of "-*+" so:
String sequence = "-*+";
String s = "+**+++****+++++";
int seqI = 0;
for(i=0; i != s.size(); ++i) {
for(j=0; j < i+2; ++j) {
System.out.print(sequence[seqI]);
}
if(seqI < sequence.size()) {
++seqI;
} else {
seqI = 0;
}
}
You can define a function like this:
public static char Output(int i, int mode)
{
if (mode == 1)
{
return (char) i;
}
else if (mode == 2)
{
if (i % 2 == 0)
{
return '+';
}
else
{
return '*';
}
}
else if (mode == 3)
{
if (i % 3 == 0)
{
return '-';
}
else if (i % 3 == 1)
{
return '*';
}
else
{
return '+';
}
}
}
And use it just like:
for (int mode = 1 ; mode < 4 ; mode++)
{
for (int i = 1 ; i < 6 ; i++)
{
for (int j = 0 ; j < i + (int)(mode / 3) ; j++)
{
System.out.println(Output(i, mode));
}
}
}
Note: Yes! Actually my code is hard to read, but if you try to read it, you will learn something more than other answers!

Categories