I'm just learning about arrays. I have a small program to write that asks to create 2 arrays. One holding 5 names of flowers. One holding the prices of those 5 flowers. I ask the user what kind of flower they want and the quantity. I then need to create a loop that locates the flower name and uses that index to find the cost of the flower. I am struggling on how to write in code how to use one array index to find another. Example: Roses and $.50.
I am trying to create a program for class the uses two dimensional arrays. I am stuck on the second step that states Use two parallel arrays. One is a two-dimensional array -- a row of this array will hold six values in this order: [0] number of hours worked, [1] hourly pay rate, [2] gross pay, [3] net pay, [4] federal withholding, and [5] state withholding.
This is what I have so far:
Java Code:
double [][] data = new double [30][6]; String [] names = new String [30]; String str = null; String detail = null; int n = input(data, names, inputFile); mh_sh_highlight_all('java'); (there's more but i don't believe it pertains to this question)
My question is how would I create this array. Or, is that right above? I've searched online and in my book and I just don't understand.
I am trying to write a java program that asks the user for a list of names (one per line) until the user enters a blank line. At that point the program should print out the list of names entered, where each name is listed only once (i.e., uniquely) no matter how many times the user entered the name in the program. I am supposed to use an ArrayList for this problem.
My idea for a solution is to create an ArrayList<String>, read each name, i.e., line, entered and then to see if that name is already in the ArrayList. I created a for loop to check each element in the ArrayList but when I try to assign an element to a string variable I get the error "Type mismatch: cannot convert from Object to String". Not sure why that is happening because the ArrayList is defined as a String list.
Write a program that asks the user to enter five test scores. The program should display a letter grade for each score and the average test score. Write the following methods in the program:
calculateAverage This method should accept five test scores as arguments and return the average of the scores. determineGrade This method should accept a test score as an argument and return a letter grade for the score, based on the following grading scale
Score Letter Grade 90-100 A 80-89 B 70-79 C 60-69 D Below 60 F
System.out.println("Enter the first score"); test1 = keyboard.nextDouble(); System.out.println("Enter the second score"); test2 = keyboard.nextDouble();
Write a program that asks the user for an integer andthen prints out all its factors in increasing order. Use a class FactorGenerator with a constructor FactorGenerator(int numberToFactor) and methods nextFactor and hasMoreFactors. Supply a class FactorTester whose main methods reads a user input, constructs a FactorGenerator object and prints the factors.
Here is what I have so far for my main class and tester class
public class FactorGenerator { int factor; int number; int x; FactorGenerator(int numberToFactor) { number = numberToFactor; }
Write a program that continuously asks for an alphabet letter, and stops when a non-alphabet letter is entered. Then output the number of upper case letters, lower case letters, and vowels entered ....
The assignment is to: Use arrays to create an applet that lists five of your favorite songs.
The applet should scroll the list of song titles, one at a timeeach song title should start at the top of the applet and scroll to the middle then, scroll off the right hand sideeach new song title should scroll in a different colourthe applet should loop, that is, when it gets to the end of the list, start over again at the beginning of the list
I already have it looping the text with different colors. It's just they all loop at the same time. How to loop them separately. How to switch the program to where the strings will change with content and color on each loop that would work too.
Here is the code I got so far:
import java.awt.*; import java.applet.Applet; public class FavouriteSongs extends Applet implements Runnable { String[] Songs = {"Travelling Man-Chameleon Circuit", "Sing-Ed Sheeran", "It's Time-Imagine Dragons", "I See Fire-Ed Sheeran", "Colder Weather- Zac Brown Band"}; int yPos = 0, xPos = 50; Thread runner;
Create a method called mirrorImage, which takes two integer arrays as input parameters. You may assume that the two actual parameters have the same length. Your method should return true if the arrays are the reverse of each other. Otherwise mirrorImage should return false.
Examples:
data1:{1,2,3} data2:{3,2,1} ==> true
[code].....
I'm pointing a place outside of the array or something
I am trying to figure out the Sieve of Eratosthenes program using arrays. I am trying to display the prime numbers of 1-100. My code right now is:
import java.text.DecimalFormat; import java.util.Scanner; public class TextLab06st { public static void main(String args[]) { // This main method needs additions for the 100 point version. Scanner input = new Scanner(System.in); System.out.println(" TextLab06
[code]...
I am getting the error <identifier? expected for the line "primes.[index] = false;"
I am designing a program that generates 3 random numbers from 1-5 and if 2 match, the user wins $1. If 3 match, the user wins respectively:
All 1s - $5 2s - $10 3s - $25 4s - $50 5s - $100
I first used a loop to run until the user runs out of money or wishes to stop. Then I made 2 methods. 1 to generate the random numbers and 1 to see if the user won any money. I am storing the random numbers in an array called slotnumber.
This is what I have so far but I am getting compiling errors at while (cont == 'y') {
import java.util.Scanner; import java.util.Random; public class SlotMachine {
I am trying to create a program that reads a sentence, such as: "abba is running to the radar" scans this sentence, and then prints out all the palindromes. I am running into issues with my arrays and for statements. Here is my code:
static String palindrome, backwardsLower, palindromeLower, palindromeClean, backwards2, backwards = ""; static String[] words; static int counter; public static void main(String[] args) { palindrome = JOptionPane.showInputDialog("Please enter a phrase. " +
[Code] ...
I am aware that there is a few "useless" variables in there at the moment, I will clean them up (as well as some useless statements, I see those too). The issue comes at about line 17. The variable backwards REMOVES all the spaces from the array, so when it comes time to compare the strings, it is comparing individual words to the ENTIRE string, thus no words will ever be a palindrome.
SlotMachine.java:21: error: illegal start of expression public static void getNums(int [] slots) ^ SlotMachine.java:21: error: illegal start of expression public static void getNums(int [] slots)
[code]....
i keep fixing small things and cannot get it to compile. Below is the original code,
import java.util.Scanner; import java.util.Random; public class SlotMachine { public static void main (String args[]) { int userMoney; Scanner input = new Scanner(System.in); System.out.print("How much money to start with?
My assignment is to essentially update a calculator program that uses strings, and methods, to include arrays. How to create and call an array that the user defines the size of and then inputs the numbers to fill the array. Here's the example my prof gave us of what the output should look like:
Menu 1. Add 2. Subtract 3. Multiply 4. Divide 5. Dot product 6. Generate random array 7. Quit
What would you like to do? 1
How many values are in the arrays? 3
Enter the values in the first array, separated by spaces: 2 4 6
Enter the values in the second array, separated by spaces: 1 3 5
The result is [3.0, 7.0, 11.0]
How to create an array that would allow the user to define the size of the array and then inputs the values for the array? I'm completely lost. I never should have taken java as an online class.
I am making a gradebook program that calculates the averages of 25 students using 2D arrays in which the students have four test grades. I have figured out how to display the averages of each student in one column but I cant seem to figure out how to display the letter grade of the average into another column.
Right now my program reads from a .txt doc
Heres what I've got.
TestGradeBook.java
import java.util.*; import java.io.*; public class TestGradeBook { public static void main (String [] args ) throws IOException{ //Declarations final int ROWS = 100; final int TESTS = 4;
[Code] .....
GradeBook_Data.txt Name100100100100 // basic name and grades
Recently I have missed a few weeks of my class due to family emergencies. Because of this I have missed very important lectures. My professor has assigned me the following assignment:
Prices Write a Java program that specifies three one-dimensional arrays names price, amount, and total. Each array should be capable of holding 10 elements. Using a loop, input values for the price and amount arrays. The entries in the total array should be the product of the corresponding values in the price and amount arrays. After all the data have been entered, display the following output:
Total Price Amount
==== ==== ======
Under each column heading display the appropriate value.
I know how to display everything using printf, how to create the actual arrays and define the array size, but I am confused on how and what loop to use and how to construct it. This is what I have managed to write up so far:
import java.util.Scanner; // Imports the Scanner class import java.text.DecimalFormat; // Imports the DecimalFormat class
public class Prices { public static void main(String [] args) { double[] price = new double[10]; double[] amount = new double[10]; double[] total = new double[10];
// Create a new Scanner object Scanner keyboard = new Scanner(System.in); } //End of main method } // End of public class
I mainly would like to know is a int[4][4] or a int [4*4] is more efficient to use. Which takes more storage? Which requires more to process? that kind of stuff.
1. Write an algorithm that asks the user for your birth date and the current date and displays how many days has passed since then (remember that April, June, September and November has 30 days, February has 29 on leap years, and the rest 31)
2. Write an algorithm that asks the user a number (save it as N) and displays the N term of the Fibonnacci series (take it as 1, 1, 2, 3, 5 ...)
Ive been trying to create a program to say how many 5cent,10 cent and 50 cent coins there are if you put a number in example 20. I used the init method because thats what the teacher told me to use but i have no clue on what to do.
I am now at the beginning trying to teach myself Java :) So far it has been good everything however now I came across the problem where I wanted to create payroll program for myslef based on one of the books I have.Somehow the issue in provided program code gives me the negative values on the salaryNett and valueTax:
Java Code:
import javax.swing.JOptionPane public class FinnancialApplicationPayroll { public static void main (String[] args){ String input = JOptionPane.showInputDialog (null, " What is your name");
We have to create a program called MyStack.In Push: Add one element at the end of the buffer.In Pull: Remove one element from the end of the buffer. In isEmpty: Should check if the list is empty or not and return the result as a boolean variable.
I am totally confused, I wrote the buffer and the array, but how do i add an remove element from the buffer?How do i add an element? What Statement do i Use?
Java Code: public class MyStack<T> implements Stack<T> { private final T[] ringbuffer; int index = 0; int last;
I have spent the past 12 hours on it and have gotten no where. I need to create a program that will calculate different tax brackets. I have tried all combinations I can think of and can't get it to work!!! I am literally about to go insane! I need the program to calculate tax will be 1% for anything up to $50000. If the tax is over $50000, you keep that 1% of $50000 and then add the remaining. So for an income in the second range, it would be 1% of $50000, and then 2% of (income - $50000). This then continues for each range.
The problem I'm having is getting it to display the correct interest rates. I am not asking for you to write it for me. I want to learn. But at this point I have exhausted all resources available to me.
import java.util.Scanner; public class Project3taxinfo { public static void main(String[] args) { final double RATE1 = 0.01; //1% tax on the first $50,000 final double RATE2 = 0.02; //2% tax on the amount over $50,000 up to $75,000 final double RATE3 = 0.03; //3% tax on the amount over $75,000 up to $100,000 final double RATE4 = 0.04; //4% tax on the amount over $100,000 up to $250,000 final double RATE5 = 0.05; //5% tax on the amount over $250,000 up to $500,000 final double RATE6 = 0.06; //6% tax on the amount over $500,000