I am doing an assignment where I have to find the price per square inch of a pizza, compare them and display the results. I have everything figured out with the values and stuff. Now when I have to displays the results I have to display which of the two pizzas is more favorable.
I have both values / square inch for both. and I know how to find the minimum value of the two wit the Math.min class. My question is how can I assign the char, PIZZA A to the value that I had so I can display it in the output statement, without writing PIZZA A. It should display after the difference is calculated.
Here's my code so far.
// This programs finds the price per square inch of a pizza
import java.util.Scanner; import java.text.DecimalFormat; public class PizzaSquareInches { public static void main(String[] args) { Scanner input = new Scanner(System.in); DecimalFormat df = new DecimalFormat("#.###");
At first I wanted to just use an array and set each day a value, however I was told that it has to be stored as a string.
Design and implement the class Day that implements the day of the week in a program. The class Day should store the day, such as Sun for Sunday. The program should be able to perform the following operations on an object of type Day:
A. Set the day. B. Print the day. C. Return the day. D. Return the next day. E. Return the previous day. F. Calculate and return the day by adding certain days to the current day. For example, if the current day is Monday and we add four days, the day to be returned is Friday. Similarly, if today is Tuesday and we add 13 days, the day to be returned is Monday. G. Add the appropriate constructors. H. Write the definitions of the methods to implement the operations for the class Day, as defined in A through G. I. Write a program to test various operations on the class Day.
import java.util.*; public class Day { static Scanner readinput = new Scanner(System.in); String day; public Day(String day) {
[Code] ....
So right now if I run my code it allows me to type in a day, then it gives me the next and previous day, the last part is to add X days to it. Ideally I would like to be able to take the day entered, depending on the day set a numeric value, then add prompt for number of days you want to add. The program should then use modal (%7 ) to add value to the value that was given based on the day, and then translate it back into a String value (the day).
import java.util.Scanner; public class grossPay { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("The Acme Company pays all their employees the same ratee"); Scanner input = new Scanner(System.in);
[Code]....
I know for sure that line 32 is wrong but I don't know why. There is a red line under the brackets.
I am having trouble with this displaying the grosspay. Why it is not displaying it right. All I am getting is 0.00 all the time, everything else seems to work great.
// This program shows the payroll coding challenge
public class PayRoll { // Constant for the number of employees public final int NUM_EMPLOYEES = 7; private double[] employeeID = {5658845, 4520124, 7895122, 8777541, 8451277, 1302850, 7580489}; // To hold hours worked private int[] hours = new int[NUM_EMPLOYEES];
I have the following code in which I am looping through the rows of one array (composed of Strings) and copying it to another array. I am using .clone() to achieve this and it seems work as it changes the memory location of the rows themselves. I did notice that the String objects are still pointing to the same location in memory in both arrays but I won't worry about that for now, at the moment I just want to understand why the array I am cloning is not successfully assigning to the other array.
This is the incorrect line: ar[r] = maze[r].clone();
I'm allowing the user to choose certain items to buy that is moved to an array.Now I'm trying to add those thing in the array use a different class. how I can call the array from my driver class to my checkout class that adds them together.
I had to use scanner to receive input from the user for a formula and everything compiled and worked correctly except when I entered a double into the Scanner, it only accepts ints and gives me this error every time I enter a double:
java.util.InputMismatchException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at java.util.Scanner.nextDouble(Unknown Source) at TestA1Q2.main(TestA1Q2.java:36) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
I know that you usually get that error when the Scanner expects something else that is different from what you type and I have looked over my code time and time again and cannot find anything. I created a test program to see if it was just that problem and not something else in the code and I still got the same error but when I tried compiling and running it here browxy.com everything worked and I got the right answer for the formula. Here's the test I used:
import java.util.Scanner; public class TestA1Q2 { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); System.out.println("Enter a double"); double velocity = keyboard.nextDouble(); } }
At this point I'm thinking it might be a problem with my install of DrJava and I've tried re-installing but nothing changed.
while(fileScan.hasNextLine()) { String currentLine = fileScan.nextLine(); String[]dataSet = currentLine.split(" "); String zip = dataSet[7]; String phone = dataSet[8]; String donation = dataSet[9]; int ZIP = Integer.parseInt(zip); Double Donation = Double.parseDouble(donation);
As you can see I made the donation read as a double, my question is how do I get the donation total? I need to write the code to add up and get the total donations but cannot figure it out.
Just done a quick test to try and figure out the difference between floats and doubles.I made a quick program which outputs the result of 3.3 * 2 as both a float and a double.
I assumed since a float is the larger and more precise of the two data types that there would be more numbers after the decimal point, however this was not the case, it was in fact the double which had more numbers after the decimal point. Result was as follows:
Float: 29.699999 Double: 29.7
BTW for the above code I simply had a few text fields and a button with the following code:
Also, as these are limited to a certain amount of numbers im thinking there must be a more precise way for currency, if so what would I use for that? I suppose what im trying to figure out is what data type to use in different scenarios? When to use integer, float, double and long.
Write a Java method that returns the largest value of an array of doubles passed to the method as an argument.
Back into java wasn't sure how to do it for doubles did one in the main for integers and then added a method changed from int to double and now i'm lost as go why its not working.
package kickstarter9; public class Kickstarter9 { public static void main(String[] args){ double myList; double[] myList = {6.0, 4.1, 2.4, 6.8, 1.9, 9.4, 2.8, 4.6, 9.3}; // find the largest value in the list
This code will let a user type 10 double's, and return the smallest of those 10 double's. But I don't really understand what happens in de last for-loop.
package hsleiden.webcat.exercise06_09; import java.util.Scanner; public class Exercise06_09 { public static void main (String[] args){ double[] allNumbers = new double [10];
I've been noticing some of my programs have been a little buggy recently, and think it's down to confusion over doubles and positioning. Lets say I have a label called banner that I want to scroll across the screen. Now I need to know the label's width in order to position it, but the width depends on the amount of text, so i use this code:
double bannerWidth = banner.getWidth();
Which forces me into using a double if I want to be accurate.
But the problem is that I'm trying to use a condition that compares the label's horizontal position (currentX) to the left edge of the screen (LEFT_EDGE), minus the width of the label (bannerWidth). In other words when the label is off the screen, it should go back to its starting position.
I assume that means that any variables I use to track the label's position (in this case currentX), or constants that I use to check equality (LEFT_EDGE), have to be doubles as well?
My difficulty is that I iterate currentX. But currentX--; won't work because doubles don't iterate as I'd expect.
So casting becomes an option..... but if I cast to an integer I effectively lose width on the label. And that is magnified each loop, resulting in the label's starting position moving further and further to the left.
I'm attempting to format my doubles to two decimal places within my return statement. I have tried to use DecimalFormat but it gives me an error because my method needs to return a double and that results in a string being returned. I have also tried using the *100.00/100.00 method and that doesn't work when the number already ends in 0.
If I pass -150.00 it gives me -150.0 when I need two decimal places.
"Create a project called RainFall and a class nameD RainFall. Write a program that stores the total rainfall for each of 12 months into an array of doubles. The program should display total rainfall for the year, the average monthly rainfall, the month with the most rain and the month with the least rain. When outputting the month with the most and least rain, output the name of the month. This is what I have so far.
simple assignment of values to a previously initialized object?
See the method useModel ()
The idea is, assign the values to the temporary object, data
Then plunk it into this statement:
model.addRow ( data );
Simple enough?
I've been putzing with the syntax for multiple hours, over days, now.
With and without
[0];,
Netbeans keeps giving me: Illegal start of expression data is declared as an array of Object, although, in this case, it does not need to be an array. What is the correct syntax?
I've been referencing my text and a few other sites to assist in building this class...and I'm still uncertain of the purpose of a few of the methds: next(), hasNext().
Also, I have not found a clear explanation of the following code example: tail.next = tail; There are several instances of this in the code below....I'm just not sure exactly how this assigns the value to the next object in the other class..?? ??
public class MySinglyLinkedList<T> implements SinglyLinkedList<T>{ protected NodeList<T> head, tail, current, newNode; String name; int size = 0; public MySinglyLinkedList(){ head = null; tail = null;
Can I assign multiple values to one variable? For example I want myNum = 0 thru 9 you see im trying to program a password checker program to verify that the password meets all the criteria 8 char long, 1 upper case, 1 lower case, 1 numeric, 1 special, and don't contain and or end