Printing stars with Java. I am stuck [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm working on with a problem where I should print stars (*) with Java. Here's the pattern that program should print: There should be three lines of stars
Line 1. 5 Stars.
Line 2. 3 Stars.
Line 3. 9 Stars.
Here's what I have a drafted so far:
private static void printStars(int number) {
System.out.print("*");
}
public static void main(String[] args) {
printStars(5);
printStars(3);
printStars(9);
}

private static void printStars(int number) {
System.out.print("*");
}
This method requires a loop to print the required number of stars, rather than just one (which is what you're doing at the moment, ignoring the parameter that's passed in.) Look up "for loops" - that's in all likelihood what you're required to use here.

Related

Java - Returning a new array containing the squares of the elements of its array parameter [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Below I have got a code that returns a new array containing the squares of the elements of its array parameter (the input array should remain unchanged). For instance, if the input array is {0,1,2,3} then the output is then {0,1,4,9}.
public static double[] square (double a[]){
double[] s = new double[a.length];
for (int i=0; i<a.length; i++) {
s[i]=a[i]*a[i];
}
return s;
}
Whenever I want to test above code, I have got following problem, I have done a screen shot and the picture is here:
Any suggestions how I can test the numbers 0,1,2 and 3 ?
Okay as I have mentioned above, I have got a code (above) and I want to:
A) Test it using 0,1,2 and 3.
B) Sadly I had a problem and I couldn't test the numbers. So as an example and describe my problem I did a screenshot. Is my problem clear for everyone now?
Okay, so I see that you're using BlueJ. I'm not familiar with that IDE, but I believe the error you're seeing: "error: <identifier> expected" is thrown for a failed compile. That usually shows up for syntactical errors like forgetting a semicolon or curly brace.
-- EDIT --
Example using JUnit4 in case you decide to go that route:
#Test(expected=NullPointerException.class)
public void testNull() {
Square.square(null);
}
#Test
public void testArray() {
double[] squares = Square.square(new double[] {0d, 1d, 2d, 3d});
Assert.assertEquals(squares[0] == 0d);
Assert.assertEquals(squares[1] == 1d);
Assert.assertEquals(squares[2] == 4d);
Assert.assertEquals(squares[3] == 9d);
}

Math calculations in java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Is there an easy way(instead of hard coding) to display intermidiate calculation in java?
For example: x+y*3= 10+15*3=55
I have searching for an answer without success.
You could do something like this -
public static void main(String[] args) {
int x = 10;
int y = 15;
System.out.printf("x+y*3 = %d+%d*3 = %d", x, y, (x + (y * 3)));
}
Which outputs
x+y*3 = 10+15*3 = 55
here.
Try to learn parsing strings as shown here: http://www.javapractices.com/topic/TopicAction.do?Id=87

java array printing the repeated numbers , array in the array [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Please could you tell me how its work in section (g[ss[i]]++;) and tell me the sequence of output in java
class A{
public static void main(String []a){
int []ss={1,2,3,4,2,3,3,1,1,1,5,6,4,5,4};
int []g=new int[15];
for(int i=0;i<15;i++){
g[ss[i]]++;
}
for(int i=1;i<15;i++){
System.out.println(ss[i-1]+"=="+g[i]);
}
}
}
Can't you run it?
g[ss[i]]++; can be rewritten as
int index = ss[i];
g[index] = g[index] + 1;
So it's counted number of each number in ss.
It's very error prone, and you should never do something like that.
Just run it?
1==4
2==2
3==3
4==3
2==2
3==1
3==0
1==0
1==0
1==0
5==0
6==0
4==0
5==0
This should be your output.

how to check if command line argument is given or not? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to code a program which set a size of integer from the command line argument.if any command line argument exist,it will set the size according that input.otherwise a default value will be set. Suppose I don't push any command line args. how can my program detect that?
Your main method should be
public static void main(String[] args) {
...
}
The array args will have length == 0

How to check the positive and negative number's in a given arraylist in android or java? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have set of int Arraylist for my application.For example
int[] array={10,8,-4,-6,5}
Here i want to check this array list.i am using this array for my bar graph chart.if all the value contains positive value,i want to show the graph in 0 to 100 % Y axis.if array contains negative value,i want to show the negative axis like -50 to 100 % depends upon the value.
How to do the logic for this problem.can anyone help me for my application?
boolean isNegative=false;
for(int i=0;i<array.length;i++)
{
if(array[i]<0)
{
isNegative=true;
break;
}
}
if(isNegative)
{
// code for displaying negative axis
}
else
{
//only display positive axis
}

Categories

Resources