Draw V-shape Of Height N Entered By User With Stars
Apr 12, 2014Am stuck in a problem that asks to write a program using nested for loops to draw a V-shape of height "n" entered by the user with stars .
View RepliesAm stuck in a problem that asks to write a program using nested for loops to draw a V-shape of height "n" entered by the user with stars .
View RepliesI have this piece of code, i want to insert a text inside my shape.
import javax.swing.SwingUtilities;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.BorderFactory;
import java.awt.Color;
[Code] ....
Basically I need to make a program prompts the user for an integer, check to make sure the length entered by the user is a power of 2 and at least 2. Then I need to convert from base e to base 2 then I need to print the tick marks of a ruler based of the value of the length.
I got the whole converting thing working and the check for a power of 2, that wasn't an issue because it didn't require any recursion. I just don't know how to print out the proper tick mark values.
The way it is supposed to work is that it needs to look like this. Say the user enters 8;
012131210
012345678
Or if the user enters 16;
01213121412131210
01234567890123456
The bottom row is pretty much just the index value, that I print fine.
The top row though is supposed to be the length of the ticks on a ruler, with the middle most value being the value of the conversion from base e to base 2 from above. I can get that printed and what I get just looks like this.
For 8;
000030000
012345678
For 16;
00000000400000000
01234567890123456
As you can see I can get the middle value and the index values below but I don't know how to use recursion to get the right numbers.
Here's my current code.
import java.util.*;
public class TickMarks {
public static void main (String args[]){
Scanner input = new Scanner(System.in);
boolean looping = true;
while(looping == true){
System.out.print("Please enter a length: ");
[Code]...
Now the methods isPowerOfTwo() and printLength() both work fine. However, I'm not sure how to get the left and right side working.
My thoughts were to split the length in half and get a left and right side. I gave both of them arrays so I can keep track of the values. Now as you've seen above I get all zeros and I know it's because that's the default value in an array, I just don't know how to get the proper values.
I am trying to create a program that will estimate a child height based on the height of the parents. It should ask the user to enter a String representing the gender, so m or M for a male and f or F for a female. The program must handle both upper or lower case gender entries. I need it to ask the user for the height of both parents in two parts:
1. for Feet and store in an int variable.
2. for inches and store and int variable.
So for example if the father is 6' 2'', the user would enter 6 when asked for feet and 2 when asked for inches. Convert the height of the each parent to inches. hint 12" in one foot.
Apply the following formulas based on gender (must use an if statement(s)):
Hmale_child = ((Hmother * 13/12) + Hfather)/2
Hfemale_child = ((Hfather * 13/12) + Hmother)/2
I cannot figure out what is missing from my code
<import java.util.Scanner;
public class ChildHeight
{
public static void main(String[] args) {
Scanner scannerObject = new Scanner(System.in);
String gender;
String male;
String female;
[Code] ....
How do you use power of math. I'm trying to write a calculator to calculate the volume of a cylinder by asking the user to enter the height and radius but when I use pow(2) it doesn't work. I imported java.lang.math class so i dont have to keep using math. for now my code runs just fine since I'm using radius * radius but I would really luv to use the power instead times each other when i have to use higher powers.
import java.util.Scanner;
import static java.lang.Math.*;
public class Lab2 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
[Code] .....
Below is my current program for my java class. The assignment is to validate a date when entered by user.
import java.util.*;
public class Date{
public static void main (String arg[]){
int month = 0;
int day = 0;
int year = 0;
[Code] ......
My issue with my program is the output for the day.
If I enter 30 for the date I get this: Date is invalid
Date is invalid
Date is invalid
Date is invalid
Date is invalid
I wanted to know how what is wrong and why it is doing this.
Before continuing a task, any more accurate way to identify hashes? Currently, I am using regex + length of hash to give the user possible hashes.
Identifier class:
package votek;
* Purpose: Class is used to run checks on possible hashes that the user has entered.
*/
public class Identifier {
//Hash entered by user is stored here
private String hash;
//String that will contain all possible hashes to share with user
private String results = "Possible Hashes: ";
//Method that runs the other hash methods checks and prints out the results.
[Code] ...
I have a GUI with a textArea for the user to input numbers, a button which should "listen" for those numbers, and then a textField to display the sum. I have my code working for user input of one number; but I'm at a loss as to what sort of loop I need to create to get it to read each line of input. I wasn't sure whether to put this in the beginner forum or here, because I am definitely a beginner.
So my code thus far that works with one input:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
[Code] ....
I need to find if the number entered by the user are consecutive or not!
Ex:this should return a true!! This should return false!
(1,2,3) (3,5,7)
(3,2,4) (1,2,2)
(-10,-8,-9) (7,7,9)
My program seems to work ok when i enter number in order like 1,2,3 = true , and all the numbers for false seem to be working as well! my problem is when i enter number like 3,2,4 that are not in order but still are consecutive!! I thought that another if statement would be the solution but i have tray several different ones and still can't make it work !!!
Java Code:
import java.util.*;
public class Consecutive{
public static void main (String [] args){
Scanner console= new Scanner(System.in);
System.out.println("Enter three numbers");
String numbers = console.nextLine();
[[Code] .....
Ask user to enter a name alphabet by alphabet, and print d whole word as output,,,,,, use while loop?Since i am new to JAVA..i have no clue on how to begin?
View Replies View RelatedI'm trying to implement a playlist class that creates a playlist from songs that the user has entered. Basically, it'll have the following requirements:
There can be a maximum of 2 playlists.
Each playlist holds a maximum of 3 songs.
Can't be over 12 mins.
Can't be over 10 MB.
Can't use arrays (a real pain).
I'm really struggling with how to even go about this. I'll post majority of my code below, I know a lot of it can be condensed/improved but I'm just trying to focus on functionality at the moment.
Interface class:
public class Interface
{
Scanner console = new Scanner(System.in);
private SongDatabase database = new SongDatabase();
private Playlist playlist = new Playlist();
private int songCount=0;
[Code] .....
In short, the aspects that are relevant to the playlist functionality are the addSongtoPlaylist() method in Interface class, the getSong() method in SongDatabase class and the Playlist class.
Java program : For this assignment you are going to write code for the following class:
MathOnThreeNumbers
Here are the specifications of class MathOnThreeNumbers:
Methods of class MathOnThreeNumbers:
1. inputThreeNumbers
2. getAverage
3. getSum
4. getNumberOne
5. getNumberTwo
6. getNumberThree
Constructor of class MathOnThreeNumbers
write a constructor that initializes the first, second, and three numbers to 1, 2, and 3 respectively.
Specs for the methods methods:
1.
name: inputThreeNumbers
accessibilty: public
arguments: none
what it does: asks the user for three numbers of type double
2.
name: getAverage
accessibilty: public
arguments: none
what it does: returns average of the three numbers
3.
name: getSum
accessibilty: public
arguments: none
what it does: returns sum of the three numbers
4.
name: getNumberOne
accessibilty: public
arguments: none
what it does: returns the first number entered by the user
5.
name: getNumberTwo
accessibilty: public
arguments: none
what it does: returns the second number entered by the user
6.
name: getNumberThree
accessibilty: public
arguments: none
what it does: returns the third number entered by the user
Here is an example of how the class MathOnThreeNumbers works. The following code produces the output displayed after the code.
MathOnThreeNumbers mm = new MathOnThreeNumbers();
System.out.println("first: " + mm.getNumberOne());
System.out.println("second: " + mm.getNumberTwo());
System.out.println("third: " + mm.getNumberThree());
mm.inputThreeNumbers();
[Code] .....
/**
* Auto Generated Java Class.
*/
import java.util.*;
public class Hangman {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
int guess;
boolean revealed[] = {false, false, false, false, false};
String word [] = {"c", "a", "n", "a", "d", "a"};
[Code] ....
I am not sure how to make the program check if the letter entered by the user matches the one in the array. also i am not sure how to make the program run again with a new word.
I have the code below that just keeps getting the user's name and displaying it until the user enter's an empty string. Well, to simulate that, I just hit the keyboard instead of entering any name but for some reasons I am not seeing in my code, the programme just keeps looping.
System.out.println("Enter your name :
");
Scanner st = new Scanner(System.in);
while(st.hasNext()){
System.out.println("Enter your name :
");
String name = st.nextLine();
System.out.println(name);
if(name==" ") break;
}
System.out.println("you are out of the while loop now!!");
I have returned with yet another problem but I think this one might be a bit easier to fix.
So the whole MO of this program is to take user input, and display the sum of the digits of the number they entered. I am supposed to do this by utilizing methods. So far this is what I have and when I compile it tells me that it "cannot find symbol", which I don't understand as I define what "m" is in the for loop. The other error is that it says:
"Exercise6_2.java:22: error: incompatible types: possible lossy conversion from long to int
return result;
^
I don't understand why it's giving me this error nor do I understand why result seems to inherently be an int. (Also the public static int sumDigits(long n) method was given to me by the book so that is what I am supposed to use).
import java.util.Scanner;
public class Exercise6_2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a integer");
long n = input.nextLong();
[Code]...
I need to write a Java program to perform a mathematical calculation between two numbers entered by the user. User has to choose the mathematical operation and input it and then when the user enters 2 numbers, he gets the answer. When user enters any other character other than *, /, + and - he should be able to exit. Perform calculation between numbers should be until user decided to exit from the program. My code is below, calculation part goes well, but can't get it exit when user enter any other character.. How to fix it?
import java.io.*;
public class q11
{
public static void main(String[]args)throws IOException
{
InputStreamReader ISR=new InputStreamReader(System.in);
BufferedReader BR=new BufferedReader(ISR);
while(true){
System.out.println("Enter..");
System.out.println("* : For multiplication");
[Code]...
package com.example.imc;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
[Code] ....
Logic her eis when user selects option1, it asks for user to enter name and as soon as user enters name, it should compar name to existing names in txt file. I have user while loop and for loop but for some reason, it doesnt compare properly as there seems to be some mistake in looping and it just read first line or you can say single line rather than comparing it with all lines in txt file. i have attached votes.txt file with this. Also, if user doesnt exists infile, it will ask user to enter vote as yes or no and add it to file and then count total number of Yes and No votes from file and compare them.
my votes.txt file looks as below with two columns namely (name and vote).
Hiren No
samir yes
bob no
rikul no
master yes
patrick no
summer yes
bhanja no
package samples;
import homework.EmployeeA;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
[code]....
I'm trying to learn Java and my current project is to write a short program to determine the factorial of a number entered by the user. I haven't looked. There may be a method that will do it, but I want to use a for loop specifically.
What I have compiles just fine. I'm actually pretty thrilled just with that. Here is what I have:
class factorial {
public static void main( String[] args) {
Scanner scan = new Scanner(System.in );
int num;
int product = 1;
[Code] ....
Write a Java program that asks the user to store 10 numbers in an array. You should then print the array in the order entered and then print in reverse order. However, instead of putting all the information in the main, you should use a class called PrintIt. The PrintIt class should have an instance variable that is an array to hold the numbers. You should have a method that will print the array in the order entered. You will also need a method to print in reverse oder. Then create a tester class that asks the user to enter 10 numbers and puts them in an array.
So far I've got this.
public class printIt {
int i;
int [] numArr = new int [10];
public printIt () {
i = -1;
[Code] .....
Output:
Enter a number:
8
34
Forward:
8
34
Reverse:
34
8
end
what i want as an number is being able to print out 10 numbers but this only lets me do two numbers. Why is that so?
The project is a program that allows the user to enter students and enter grades for each student. One of the requirements is that if there is already a grade stored for the student that it will display the previous grade. IF the user then enters a new grade the new grade will be stored. IF the user simply presses enter (enters an empty string) nothing is done. I have everything working except for the requirement of doing nothing if the user enters an empty string. If I just press enter at this point I get a NumberFormatException.
The below code is a method "setTestGrades" from the student class. This method iterates through each student object stored in an array list and then checks if there is a previous grade (Requirement# unset grades have to default to -1) before allowing the user to set a new grade.
public void setTestGrades(int testNumber) { //Sets the grade for the specified test number for each student in the arraylist.
testNumber -= 1;
Scanner input = new Scanner(System.in);
for (int i = 0; i < studentList.size(); i++) {
System.out.println("Please enter the grade for Test #" + (testNumber + 1) + " For Student " + studentList.get(i).getStudentName());
[code]....
This is what he wants: He wants us to prompt the user to input x values and y values (that will be entered into an array when clicking one of the two buttons (this one will say: Add point)). When they click the second button (draw lines), it should take all the points and draw a line that connects all the points together using a method that we write. In the method we have to call the paint method up which the lines will be drawn. The lines drawn should be touching every point added. It shouldn't have parameters. It will also use Graphics painter = getGraphics(); We can't use frames either.
I have everything up until the method understood.
How to write a method that will draw a line from points inputted that is called up in the action listener when the second button (draw lines) is pressed.
I'm having trouble with the last few lines of the code. It's supposed to take a replacement string entered by the user and print out the new string. For some reason it's now allowing me to enter a replacement string
import java.util.Scanner;
public class Project02 {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a long string: ");
String lString = keyboard.nextLine();
[Code] ....
Output:
Enter a long string: the quick brown fox jumped over the lazy dog
Enter a substring: jumped
Length of your string: 44
Length of your substring: 6
Starting position of your substring in string: 20
String before your substring: the quick brown fox
String after your substring: over the lazy dog
Enter a position between 0 and 43: 18
The character at position 18 is x
Enter a replacement string: Your new string is: the quick brown fox over the lazy dog <------ isn't taking user input
So I'm studying for a test i have coming up on recursion so i was playing around with it. I ended up making a pattern of stars that look that such :
*
**
***
The code i had for that was
public void makePattern(int size){
if(size == 0){
System.out.println();
}
else if(size > 0){
System.out.print("*");
makePattern(size - 1);
}
}
I was wondering if i wanted to expand on this and make it do something like
*
**
***
**
*
What condition would i have to add i was struggling trying to figure it out...
I just recently started programming and came across the star patterns which normally I am able to do extremely quickly except with this one. I am trying to make a hourglass but I seem to only be able to make the top of it. How to do the opposite so I can finish the hourglass.
package hourglass;
class Hourglass {
public static void main (String[] arguments){
for(int j=1;j<=5;j++) {
for(int i=1;i<=j;i++)
[Code] .....
KtMok1t.jpg
Below is what I go so far, but how to do star C and E.
public class PrintTriangle
{
public static void printStars (int star)
{
for (int number = 0; number < star;number ++)
{
System.out.print("*");
[Code] ....