how to draw an hourglass in java - java

I just need some pointers. I am new to java and busy with a task that prints am hourglass. what i am trying to do is write the program so that it will accepts a char from a user and then output an hourglass in the console. this is the instructions.
In this question, you are tasked to complete the implementation of the printHourglass(int, char) method for printing out an hourglass of a specific size and symbol.
Instructions
Given the parameters size = n and the character symbol.
You can assume that n is a positive odd number.
The hourglass consists of n lines.
The character symbol will appear a number of times in each line. The symbol will appear n times in the first line. The number of times the symbol printed will be decreased by 2 in each subsequent line until it reaches 1. After that, the number of times the symbol printed will be increased by 2 in each subsequent line until it reaches n again.
Spaces are added to the start and end of each line so that the total width of each line is n and the symbols in each line are aligned at the center of each line.
For outputting, you can use either System.out.print()/println() or IO.output()/outputln().
i have seen quite a few posts online but none of then take input or asks for user input or how big the hourglass should be. One that i found that works is this method to output the hourglass
public static void stars(int n, int s){
if(s > 0){
System.out.print(" ");
stars(n, s-1);
} else if (n > 0){
System.out.print("*");
stars(n-1, s);
} else {
System.out.println();
}
but this will only print the it with an asterix. i was thinking of starting my method like this
public static void printHourglass( int size, char symbol)
i obviously dont want to copy other peoples code so please give me some pointers as to how i can get his method to work.
thanks

You can use Recursion.
To print a line, you need the following
The character to draw the hourglass (char character)
The odd number you read from the user ( int oddNumber)
The index of the current line being drawn (int currentLine)
For any odd number, say n, hourglass has n lines.
Code:
import java.util.Arrays;
public static void main(String[] args) throws java.lang.Exception {
int oddNumber = 5;
char character = '#';
hourglass(oddNumber, character, 0);
}
public static void hourglass(int oddNumber, char character, int currentLine) {
if (currentLine == oddNumber) {
return;
}
int patternLength = 0;
int mid = (oddNumber + 1) / 2;
if (currentLine < mid) {
patternLength = oddNumber - (currentLine * 2);
} else {
patternLength = 2 * (currentLine - mid + 1) + 1;
}
char[] whitespace = new char[(oddNumber - patternLength)/2];
Arrays.fill(whitespace, ' ');
char[] pattern = new char[patternLength];
Arrays.fill(pattern, character);
System.out.println(new String(whitespace) + new String(pattern));
hourglass(oddNumber, character, currentLine + 1);
}

I get the code from the Creating an hourglass using asterisks And changed it little bit so you can give character. You can change the name of draw(int w , char c) method to the method name that is given to you like printHourglass(int w, char c)
public static void draw(int w , char c) {
draw(w, 0, c);
}
public static void draw(int W, int s, char c) {
stars(W, s,c);
if (W > 2) {
draw(W - 2, s + 1, c);
stars(W, s,c);
}
}
public static void stars(int n, int s, char c) {
if (s > 0) {
System.out.print(" ");
stars(n, s - 1, c);
} else if (n > 0) {
System.out.print(c);
stars(n - 1, s, c);
} else {
System.out.println();
}
}

Related

Java recursion 1234 to 4321 for example

I have a question how to better tackle this task, I have a version, but I am sure there is a better and shorter way to do this maybe. I need to take any int number(return it as an int without turning it into a String), but never with a 0 at the end (100, 120) but like 1234, or 4132. I need to take this number and using recursion rewrite it the other way around example 1234 to 4321, 4132 to 2314, maybe there is a way this is called, i personally don't know about it.
Here is what I got:
public static int reverse(int r, int n, int k){
if(r==0)
return 0;
else
return + (r%10) * (int)Math.pow(10, (n-k-1))+reverse (r/10, n, k+1)
}
public static void main(String[] args) {
System.out.println(reverse(1234, 4, 0));
}
Working with a String representation of the int may make the code more readable.
Try:
Integer.parseInt(new StringBuilder(r+"").reverse().toString());
Current code doesn't compile. Added a ) to this line:
from if(r==0{ change to if(r==0){
and added a ; in this line return + (r%10) * (int)Math.pow(10, (n-k-1))+reverse (r/10, n, k+1);
Your code after this two changes will look like:
public static int reverse(int r, int n, int k){
if(r==0)
{
return 0;
}else{
return + (r%10) * (int)Math.pow(10, (n-k-1))+reverse (r/10, n, k+1);
}
}
if the number ends with 0, the program will not show any special message to the user, i.e 1230 will return 321. In this case, maybe
maybe print a message ("number must not end with a 0) or throw an exception?
Didn't notice the recursion part.
public static void main(String[] args) {
int i = 589;
System.out.println(reverse(i));
}
public static int reverse(int k){
if(k/10 == 0){return k;}
else return (k%10 * (int)Math.pow(10, (int)Math.log10(k))) + reverse(k /10);
}
Explanation:
k%10 gives you the last digit of an int
(int) (Math.log10(k)) returns
number of Digits in an Integer minus one
public static final int reverse(int number) {
final int lastDigit = number % 10;
final int length = (int) Math.log10(number);
return (number < 10) ? number : (int) (Math.pow(10.0, length) * lastDigit + reverse(number / 10));
}
If the number is lower then 10 it's the number itself. Otherwise it's the last digit multiplied with 10^n where n is the length of the number, so it's now at the position for the first digit.
Then add the result of a reverse of the rest number to it (the number without the last digit).
You take advance of the recursion function itself as it would already work to solve the big problem. You only have to think about the trivial end condition and one single step (which mostly is something you would suggest as the last step)
This is the best way that I could make it using recursion and without conversions.
private static int myReverse(int n, int r) {
if(n == 0)
return r;
int newR = r*10 + n%10;
return myReverse(n/10, newR);
}
What I'm doing here is:
Two parameters: n - the number you want to reverse, r - the reversed number
The recursion stops when n equals 0 because it always dividing it by 10
newR - This variable is unnecessary but it´s better for 'understanding' porposes, first I multiply r by 10 so I can sum the last value o n. For example, reverse 123: along the way if r = 12 and n = 3, first 12*10 = 120, n%10 = 3 then r*10 + n%10 = 123
A 'pleasant' way with only one return statement:
private static int myReverse2(int n, int r) {
return n == 0 ? r : myReverse2(n/10, r*10 + n%10);
}

Averaging two characters based on their ASCII value

I am new to java programming. I am having a problem on how to use the method for char here. What do you return in the char method to get the average of the ASCII values in the main body? Thanks!
public static int average(int i,int j)
{
return (i + j) / 2;
}
public static double average(double a,double b)
{
return (a + b) / 2;
}
public static char average(char first,char second)
{
return ?;
}
public static void main(String[] args)
{
char first = 'a', second = 'b';
int i = 5, j = 12;
double a = 5.5, b = 8.5;
System.out.println("Average of (x,y) is : " + average(first,second));
System.out.println("Average of (a,b) is : " + average(a,b));
System.out.println("Average of (i,j) is : " + average(i,j));
}
Chars are at the end ints, so the average char makes no much sense since the result will be char again, i.e consider this case:
what is the average between 'a' and 'b'? a is represented with 97, b with 98, ave = 97.5 but there is no char value for 97.5, in fact, that will be rounded to int pointing to 97 again, so average for 'a' and 'b' is 'a', kind of weird isnt ?
Anyway you can do
public static char average(char first,char second)
{
return (char) ( (first + second) / 2);
}
note that since dividing by int literal 2 you will need to cast the result to char again..
In Java, char values are automatically typecasted to int type.
As you have already got average(int i,int j), you don't need to write any average(char first,char second).
So when you call average(first,second)), then the method which take int arguments i.e., average(int i,int j) will be invoked.

Java8-StringIndexOutOfBoundsException

This is my code snippet:
import java.util.Scanner;
public class Numrs {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int num = in.nextInt();
int pos, i = 1;
char d;
while (num <= 10) {
String line = in.nextLine();
pos = line.length();
if (line.length() <= 1000000) {
if (line.charAt(i) == 'R') {
line.replace('R', 'K');
i++;
}
if (line.charAt(i) == 'K') {
line.replace('K', 'R');
i++;
}
num++;
}
}
}
It's showing a StringIndexOutOfBoundsException when I enter a single digit number like 3, and even if a 2 digit number is entered, it's getting terminated without reading the string. Please help.
Well, if you enter only one character /digit, then size will be 1 but it will be placed in index 0. and you are trying to do line.charAt(i) where i=1
There are many problems in this approach.
1. The input you need to pass is more than a digit, like 3 this works because after the 3 your in expects some string.
you are not updating num. So it will end up in an infinite loop
This won't work if you give any value more than 10. I m not sure what you wanted to achieve.
Start i from 0, Developers start with 0 :)

Binary to decimal in java using only recursion (no loops)

I can not seem to get my method to convert the binary number to a decimal correctly. I believe i am really close and in fact i want to use a string to hold the binary number to hold it and then re-write my method to just use a .length to get the size but i do not know how to actually do that. Could someone help me figure out how i'd rewrite the code using a string to hold the binary value and then obtain the decimal value using only recursion and no loops?
This is my full code right now and i won't get get rid of asking for the size of the binary and use a string to figure it out myself. Please help :)
package hw_1;
import java.util.Scanner;
public class Hw_1 {
public static void main(String[] args) {
int input;
int size;
Scanner scan = new Scanner(System.in);
System.out.print("Enter decimal integer: ");
input = scan.nextInt();
convert(input);
System.out.println();
System.out.print("Enter binary integer and size : ");
input = scan.nextInt();
size = scan.nextInt();
System.out.println(binaryToDecimal(input, size));
}
public static void convert(int num) {
if (num > 0) {
convert(num / 2);
System.out.print(num % 2 + " ");
}
}
public static int binaryToDecimal(int binary, int size) {
if (binary == 0) {
return 0;
}
return binary % 10
* (int) Math.pow(2, size) + binaryToDecimal((int) binary / 10, size - 1);
}
}
Here is an improved version
package hw_1;
import java.util.Scanner;
public class Hw_1 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter decimal integer : ");
int input = scan.nextInt();
convert(input);
System.out.println();
System.out.print("Enter binary integer : ");
String binInput = scan.next();
System.out.println(binaryToDecimal(binInput));
}
public static void convert(int num) {
if (num>0) {
convert(num/2);
System.out.print(num%2 + " ");
}
}
public static int binaryToDecimal(String binInput){
int len = binInput.length();
if (len == 0) return 0;
String now = binInput.substring(0,1);
String later = binInput.substring(1);
return Integer.parseInt(now) * (int)Math.pow(2, len-1) + binaryToDecimal(later);
}
}
Don't parse binary in reverse order.
Here by calling binaryToDecimal(int) it will return decimal number.
public static int binaryToDecimal(int binary) {
return binaryToDecimal(binary, 0);
}
public static int binaryToDecimal(int binary, int k) {
if (binary == 0) {
return 0;
}
return (int) (binary % 10 * Math.pow(2, k) + binaryToDecimal(binary / 10, k + 1));
}
If you are coding just to convert numbers (not for practice). Then better approach would be to use Integer.parseInt(String, 2). Here you will have to pass binary number in the form of String.
If you are looking to do this using a String to hold the binary representation, you could use the following:
public static int binaryToDecimal(String binaryString) {
int size = binaryString.length();
if (size == 1) {
return Integer.parseInt(binaryString);
} else {
return binaryToDecimal(binaryString.substring(1, size)) + Integer.parseInt(binaryString.substring(0, 1)) * (int) Math.pow(2, size - 1);
}
}
How this works, is with the following logic. If, the number you send it is just 1 character long, or you get to the end of your recursive work, you will return just that number. So for example, 1 in binary is 1 in decimal, so it would return 1. That is what
if (size == 1) {
return Integer.parseInt(binaryString);
}
Does. The second (and more important part) can be broken up into 2 sections. binaryString.substring(1, size) and Integer.parseInt(binaryString.substring(0, 1)) * (int) Math.pow(2, size - 1). The call made in the return statement to
binaryString.substring(1, size)
Is made to pass all but the first number of the binary number back into the function for calculation. So for example, if you had 11001, on the first loop it would chop the first 1 off and call the function again with 1001. The second part, is adding to the total value whatever the value is of the position number at the head of the binary representation.
Integer.parseInt(binaryString.substring(0, 1))
Gets the first number in the current string, and
* (int) Math.pow(2, size - 1)
is saying Multiple that by 2 to the power of x, where x is the position that the number is in. So again with our example of 11001, the first number 1 is in position 4 in the binary representation, so it is adding 1 * 2^4 to the running total.
If you need a method to test this, I verified it working with a simple main method:
public static void main(String args[]) {
String binValue = "11001";
System.out.println(binaryToDecimal(binValue));
}
Hopefully this makes sense to you. Feel free to ask questions if you need more help.
Here is the clean and concise recursive algorithm; however, you'll need to keep track of some global variable for power, and I have defined it as a static member.
static int pow = 0;
public static int binaryToDecimal(int binary) {
if (binary <= 1) {
int tmp = pow;
pow = 0;
return binary * (int) Math.pow(2, tmp);
} else {
return ((binary % 10) * (int) Math.pow(2, pow++)) + binaryToDecimal(binary / 10);
}
}
Note: the reason, why I introduce pow, is that static field needs to be reset.
Just did the necessary changes in your code.
In this way, you would not require the size input from the user.
And,both the conversions of decimal to binary and binary to decimal would be succesfully done.
package hw_1;
import java.util.Scanner;
public class Hw_1 {
public static void main(String[] args) {
int input;
int size;
Scanner scan = new Scanner(System.in);
System.out.print("Enter decimal integer: ");
input = scan.nextInt();
convert(input);
System.out.println();
System.out.print("Enter binary integer : ");
input = scan.nextInt();
System.out.println(binaryToDecimal(input));
}
public static void convert(int num) {
if (num > 0) {
convert(num / 2);
System.out.print(num % 2 + " ");
}
return -1;
}
public static int binaryToDecimal(int binary) {
if(binary==0)
{
return 0;
}
else
{
String n=Integer.toString(binary);
int size=(n.length())-1;
int k=(binary%10)*(int)(Math.pow(2,size));
return k + binaryToDecimal(((int)binary/10));
}
}
}

Using Recursion to reverse an integer without the use of strings

I have been trying this for some time now but could not get it to work. I am trying to have a method to reverse an integer without the use of strings or arrays. For example, 123 should reverse to 321 in integer form.
My first attempt:
/** reverses digits of integer using recursion */
public int RevDigs(int input)
{
int reverse = 0;
if(input == 0)
{
return reverse;
}
int tempRev = RevDigs(input/10);
if(tempRev >= 10)
reverse = input%10 * (int)Math.pow(tempRev/10, 2) + tempRev;
if(tempRev <10 && tempRev >0)
reverse = input%10*10 + tempRev;
if(tempRev == 0)
reverse = input%10;
return reverse;
}//======================
I also tried to use this, but it seems to mess up middle digits:
/** reverses digits of integer using recursion */
public int RevDigs(int input)
{
int reverse = 0;
if(input == 0)
{
return reverse;
}
if(RevDigs(input/10) == 0)
reverse = input % 10;
else
{
if(RevDigs(input/10) < 10)
reverse = (input % 10) *10 + RevDigs(input/10);
else
reverse = (input % 10)* 10 * (RevDigs(input/10)/10 + 1) + RevDigs(input/10);
}
return reverse;
}
I have tried looking at some examples on the site, however I could not get them to work properly. To further clarify, I cannot use a String, or array for this project, and must use recursion. Could someone please help me to fix the problem. Thank you.
How about using two methods
public static long reverse(long n) {
return reverse(n, 0);
}
private static long reverse(long n, long m) {
return n == 0 ? m : reverse(n / 10, m * 10 + n % 10);
}
public static void main(String... ignored) {
System.out.println(reverse(123456789));
}
prints
987654321
What about:
public int RevDigs(int input) {
if(input < 10) {
return input;
}
else {
return (input % 10) * (int) Math.pow(10, (int) Math.log10(input)) + RevDigs(input/10);
/* here we:
- take last digit of input
- multiply by an adequate power of ten
(to set this digit in a "right place" of result)
- add input without last digit, reversed
*/
}
}
This assumes input >= 0, of course.
The key to using recursion is to notice that the problem you're trying to solve contains a smaller instance of the same problem. Here, if you're trying to reverse the number 13579, you might notice that you can make it a smaller problem by reversing 3579 (the same problem but smaller), multiplying the result by 10, and adding 1 (the digit you took off). Or you could reverse the number 1357 (recursively), giving 7531, then add 9 * (some power of 10) to the result. The first tricky thing is that you have to know when to stop (when you have a 1-digit number). The second thing is that for this problem, you'll have to figure out how many digits the number is so that you can get the power of 10 right. You could use Math.log10, or you could use a loop where you start with 1 and multiply by 10 until it's greater than your number.
package Test;
public class Recursive {
int i=1;
int multiple=10;
int reqnum=0;
public int recur(int no){
int reminder, revno;
if (no/10==0) {reqnum=no;
System.out.println(" reqnum "+reqnum);
return reqnum;}
reminder=no%10;
//multiple =multiple * 10;
System.out.println(i+" i multiple "+multiple+" Reminder "+reminder+" no "+no+" reqnum "+reqnum);
i++;
no=recur(no/10);
reqnum=reqnum+(reminder*multiple);
multiple =multiple * 10;
System.out.println(i+" i multiple "+multiple+" Reminder "+reminder+" no "+no+" reqnum "+reqnum);
return reqnum;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int num=123456789;
Recursive r= new Recursive();
System.out.println(r.recur(num));
}
}
Try this:
import java.io.*;
public class ReversalOfNumber {
public static int sum =0;
public static void main(String args []) throws IOException
{
System.out.println("Enter a number to get Reverse & Press Enter Button");
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String input = reader.readLine();
int number = Integer.parseInt(input);
int revNumber = reverse(number);
System.out.println("Reverse of "+number+" is: "+revNumber);
}
public static int reverse(int n)
{
int unit;
if (n>0)
{
unit = n % 10;
sum= (sum*10)+unit;
n=n/10;
reverse(n);
}
return sum;
}
}

Categories