Okay, thanks!
So, if I just saycreateInvoiceItems()
, that would accomplish what I'm trying to do?
Have you tried it?
Okay, thanks!
So, if I just saycreateInvoiceItems()
, that would accomplish what I'm trying to do?
Have you tried it?
Can you post the scientific formula that you wan to use. Also indicate which of the elements of the formula are the distance and the time
This '%' gives the remainder of a number. An even number is the one that can devided by 2 so the remainder of 2 should be 0:
if (number%2==0) {
System.out.println("Number: "+number + " is even.");
}
The link can be:
<a href="somepage.jsp?param1=aaaaa¶m2=3333¶m3=123">Link 3</a>
And at the somepage.jsp, you can do this:
String a1 = request.getParameter("param1"); // aaaaa
String a2 = request.getParameter("param2"); // 3333
String a3 = request.getParameter("param3"); // 123
In the same way you do it in any other class with any other object.
In one class make that object as a global attribute and either set it to be public or use get/set methods.
public class OneFrame {
public SomeObject so = null;
// create it in this class and do things with it
public OneFrame() {
so = new SomeObject();
}
}
Since you didn't explain your problem very well, I am going to assume that when you click a button in this frame another opens and you want that object to be passed to the other frame?
Then at the second frame have the constructor take as parameter that object and pass the one you have at first frame as parameter.
public class OneFrame {
public SomeObject so = null;
// create it in this class and do things with it
public OneFrame() {
so = new SomeObject();
}
private void openNewFrame() {
NewFrame nf = new NewFrame(so);
}
}
public class NewFrame{
public SomeObject so = null;
public NewFrame(SomeObject so) {
this.so = so;
}
}
I don't think you can edit that. It is very difficult to do it and since it is auto generated you shouldn't. Better start over.
I prefer not to use the GUI builder because it does not allow to create dynamic gui components (a variable number of buttons during run time)
It is better to use it to align the components you want at the Frame and then write code that sets their properties.
Where did you wanted to add the scrollbar. For component such as this gui builders can be tricky. I use them only for textboxes or single buttons.
Never read more in the specs than they state. The specs state to output that text to screen, so that's what you write.
What the author of the specs intended has nothing to do with that, if they signed off on that document, that's what they get unless they pay extra.
That is a valuable lesson to all programmers out there.
In real world Katana24, you have no idea how much context is missing when a programmer tries to understand the requirements that a non programmer has written. Half of them cannot be done and the other half mean the exactly opposite.
And to Tarli14: Look at the API for the String class and post some code. Otherwise the solution you will get is this:
System.out.println("Enter name: Ferrial P. Esber");
System.out.println();
System.out.println("A - 1\nB - 1\nE - 2\nF - 1\nI - 1\nL - 1\nP - 1\nR - 3\nS - 1\nSpace - 2");
The method createInvoiceItems is declared inside the class ProcessInvoice class. But you are doing this: invoice.createInvoiceItems()
The Invoice class doesn't have the createInvoiceItems method the ProcessInvoice has.
Also I don't think it can be called from outside the class ProcessInvoice if it is private.
Also I suggest that you initial the invoiceTotal to zero in order to avoid certain warnings
Where are you having problems? Reading the file or doing the formula.
The above are 2 different things.
First write a method that reads a file and returns a double array. I assume that all data in the file have 10 seconds difference.
public double [] read(File file) {
}
Search this forum for examples on how to read a file using BufferedReader or the Scanner class and post your code. Test it and print the array. Once you are done with that, you have your array and simply write a for loop that applies the formula described:
// Y'k = (Yk+1 - Yk-1) / 2h
result[k] = (array[k+1] - array[k-1]) / (2*h);
The above will be in a for loop
First of all don't do this:
public Card() {
Card card = new Card();
}
When you try to create an object you will call the constructor. Inside it you call this: Card card = new Card();
, which will call the constructor, but inside it you call this: Card card = new Card();
, which will call the constructor, but inside it you call this: Card card = new Card();
, which will call .......
Do you see where I am getting to?
Also about your error. The this.card is an array, but these:
card[0][x], card[1][y] are Strings, so you can't assign a String to an array:
String [][] card;
card = "Clubs"+"Six";
Take a look at your brackets. Always use them. You are suppose first to check the input and then calculate:
for () {
if (not a digit) {
return false;
}
}
// if the above code didn't return then the input is OK
But you do this:
for ()
if (not a digit) {
return false;
} else {
// calculate;
}
Since you didn't put brackets at the for-loop it will loop the next command which is not the "if" statement, but the "if-else" statement. The above code that you wrote is actually this:
for () {
if (not a digit) {
return false;
} else {
// calculate;
}
}
Meaning that calculate is inside the for-loop. Meaning that you will check each digit but do the calculation anyway:
IF input is: "a1"
The the "a" is not a digit, so the code will exit.
But when you enter: "1a", the "1" is a digit so it will go at the else, which will do the calculate and give you an error.
So always use brackets even for single commands and use the first code that I posted.
After looking at the other post with the same question, please eshirley answer me this.
Why didn't you post the code you posted here to that thread. In general why didn't post the entire first post of this thread to that. It would have been acceptable. You asked for a question and depending on the answer you got at that thread, you came up with that piece of code and you wanted to ask a question about it.
Please, I am waiting for the response.
Also did you run the code and what results did you get. Don't post code and ask if it is correct, when you can easily run it for yourself and see if it does what you want.
And one last thing.
You must understand that all forums have rules which anyone must follow when posting to them. And in any forum when you break the rules you will be asked to follow them.
One of them is not to make 2 threads that ask the same thing. It might be mistaken for spamming. So don't get upset when you break the rules that you accepted when registering here.
And on a more personal note, your question sounded like you wanted to see if the code works so you can give it to the student that asked for help. That is against the forum rules and as a teacher you shouldn't been doing this. You should been …
What is stopping from writing java code that calculates this:
// convert rate to monthlyrate
// monthlyrate = rate / 1200; // 12 for the number of months and 100 to make a percentage
// calculate monthly payment
// monthlypayment = loanAmount * monthlyrate / ( 1 - 1 (Math.pow(1 + monthlyrate, numberOfYears * 12));
// display monthly payment
// calculate yearly payment
// yearly = monthlypayment * 12;
You think the loop is there just for no reason?
String loanAmountString = JOptionPane.showInputDialog("Enter loan amount:");
String loanPeriodString = JOptionPane.showInputDialog("Enter loan period:");
double loanAmount = Double.parseDouble(loanAmountString);
int loanPeriod = Double.parseDouble(loanPeriodString);
System.out.println("Loan Amount: "+loanAmount);
System.out.println("Number of Years: "+loanPeriod);
for (rate = 5.0; rate <=8.0; rate=rate + 0.125) {
}
If you are familiar with exceptions then this would be better, but you can leave it if it confuses you:
double loanAmount = 0.0;
int loanPeriod = 0;
try {
loanAmount = Double.parseDouble(loanAmountString);
loanPeriod = Double.parseDouble(loanPeriodString);
} catch (NumberFormatException nfe) {
System.out.println("Invalid number: "+nfe.getMessage());
System.exit(1);
} catch (Exception e) {
System.out.println("Error: "+e.getMessage());
e.printStackTrace();
System.exit(2);
}
Inside the loop do the calculations
Now I have this:
package captaincrunch; public class Main { public static void main(String[] args) { // TODO code application logic here String alphabet = "abcdefghijklmnopqrstuvwxyz"; char letter; for (int index = 0, size = alphabet.length (); index < size; index++) { letter = alphabet.charAt ((index + 13) % size); System.out.println ("Original: " + alphabet.charAt (index) + ", Encoded: " + letter); } } }
I need to generalize the method so that instead of adding 13 to the
letters, it adds any given amount.
Then put the code in a method with one argument and replace the 13 with that argument.
Then call the method with whatever argument you want.
Hey, thanks! I'm not up to speed with the classes you've used or with coding for exceptions, but I'll go through the API and see what I can learn… Thanks again.
Oh, another quick question… are the methods and constructors given in the API specific enough? I shouldn't run into any issues if I stick with those right?
The API is the official reference. This is where you needto look. It explains exactly what are the arguments of a method/constructor and what they return and most importantly what they do exactly. So don't assume that you know what a method does, just read the API for a more detailed description.
i think you have not extended the taxpayer class from other main method class and also the both classes shud be in same package
You couldn't be more wrong. Please don't give wrong advices if you don't know what you are talking about.
Do all Social-Security-Numbers(SSN) need to be like this: (111...111, 222....222, ...) ?
Since you can have 999999999 different combinations, why do you want to limit yourself by using only those, when you can do this:
for (x = 0; x < 10; ++x)
{
somePayer[x] = new Taxpayer((x+1), 0.0);
}
The above will print:
1
2
3
4
....
If you want them to be:
000000001
000000002
000000003
000000004
Then don't use an int variable, use a String. Ints are used for calculations and you are not going to do any addittions with the SSN.
So this is my suggestion:
//Date: 3/18/2010
public class Taxpayer
{
private String socSec;
private double yearlyGross;
public Taxpayer(int taxPayer, double income)
{
convertToString(taxPayer);
yearlyGross = income;
}
public String getSocSec()
{
return socSec;
}
public double getYearlyGross()
{
return yearlyGross;
}
private convertToString(int i) {
socSec = String.valueOf(i);
while (socSec.length()<9) {
socSec = "0"+socSec;
}
}
}
In that wat when you call the constructor with an int: 1, then it will become the Stirng "0000000001"
It looks like it should work but have you tested it? Also you will need to take each element of the array and check if it is between 1-12 and 1-31. You should also check depending on the month if it has the irght number of days. (April cannot have 31 days). Also depending on the year and the month you might want to check if February has the right nunmer of days.
For validating String that represent dates better use this:
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yy");
sdf.setLenient(false);
String input = "something";
try {
Date d = sdf.parse(input); // if the string is not a dd-MM-yy date it will throw an exception
this.date = input;
} catch (ParseException e) {
// error
this.date = "00-00-00"
}
If you are a beginner then this is too much for you, but you can try.
First create the Class that was described with get/set methods and post the code. Then put it aside.
Then write in a main method code that reads a file and print each line. Use the Scanner class:
Scanner scan = new Scanner(new File("file.txt"));
while (scan.hasNextLine()) {
String line = scan.nextLine();
// print the line
}
Look at the API of the Scanner class. Just search it at the internet.
Use BufferedWriter that takes argument a FileWriter. Check their API
java.uti.Vector or a list, or an array, or any other form of data structure were you can store them in order to sort them
Create a Class with attributes the line and the last column of the line:
public class YourClass {
private String line = null;
private int lastColumn = 0;
}
Read each line of the file. Use the split method of the String class and take the last "element" that is your last column. Then for each line create an object of the above class, with those values and put them into a Vector.
Now you have a Vector with objects like the above. So you can sort based on the attribute int lastColumn
.
For sorting object look at the Comparable interface. Write your own algorithm or use the sort method of the Arrays class
Write the for loop like this:
for (int i=0;i<cArray.length;i++) {
// take each element of the array and [B]change it[/B]
}
Then after the loop is done, you will have the array with changed values.
Now check the constructors at the String API and see what you can do with that charArray.
Also the reason why you used such loop instead of your own is because the exercise says that only the 1,3,5, ... elements should change.
Well then start the loop at int i=1;
and instead of incrementing the 'i' by 1: ;i++
do it by 2
Actually you call the nextLine, if the previous call was a next() call or a nextInt() call.
I will review the code later. What you can do now is to print (System.out.println) the vehicles just before you display them. If they appear at the console but not at the gui, it would mean that there is a problem with the gui, else there is a problem with reading the file.
What code do you use to save them and read them?
You should also keep the "implements Serializable"
I haven't read the code, so I don't know what you are doing. But I do know if you want to serialize an object that object, whether you defined the class or it is a java class, must implement the Serializable interface:
public abstract class Vehicle implements java.io.Serializable {
}
The other vehicles that extend that class will also inherit the Serializable.
Since this is your exam it means that you attended a whole semester of your java course. Surely you would know how to create classes with attributes.
Well the instructions are pretty clear:
Create a Class
.....
EmployeePay (parent)
define: attribute: salary(net), gross, rate, no of hours worked, tax=20%
constructor: (2)
You would advise you to follow them
so you mean create another 10X10 and put x and b into it or? (im kinda new at java and this being my 3rd lab im kinda confused so sorry if im annoying you)
You need to start thinking on your own.
Can you put char inside an int array? No.
Also you haven't explained your problem properly. Since you say that you need to randomly put characters into an array you should have thought that you needed a char array. So what the code posted has to do with anything? Maybe it was an example needed modifying.
how do i convert a string to a char array??
Look at the String API as you have been instructed so many times. You do know how to call methods and loop arrays, don't you? Well search for the method described at the API. Or was the problem that you needed someone to post the link.
You cannot change the value of a specific char ina String. Convert it to a char array and loop the array. Change the elements of the array and convert it back to a String.
Check the API of the String and the Character.
For taking the 1,3,5, ... index, at loop increase the index by 2
Then you need to put the 'x' and 'b' into an array.
What do you intend to do with the value of the random. Aren't you going to populate the array?
Do you understand why it prints 'g' ?.
If you take 'b' and add 5: c,d,e,f,g you will get g
Now as for the 103. Well , what would have it print? It is an int and it must print an int number. Why are you surprised.
Try this:
int a = 40;
System.out.println( (char)a );
I believe, if memory serves me well, that these int numbers are the ASCII code of the character.
Math.random method. Check the api. It returns a random number between 0 and 1: [0,1)
How to call methods is in every basic java book there is. Instead of asking someone to tell you that, do some studying.
If you want to ask the user for input then use the Scanner class. Search for examples in this forum.
Check the String api. There is a method that creates an array of chars. Take the element at the 5th position and increase it by 5. Then use again the String api to find a method (or a constructor) and create a new String out of the char array that you changed.
To increase it is easy:
char old = 'b';
int i = old+5; // b c d e f g
char ch = (char)i;
System.out.println(i);
System.out.println(ch);
No I am saying the String class doesn't have such an attribute or method. Whether you need to create one or not it is up to you.
loop the array and call the rand.nextInt(3). You use 3 because you said you want 1/3 and 2/3. If the result is 0 then put 'x' in the array. If it is 1 or 2 then put 'b'.
Of course the array would be an array of chars
So far the code seems ok. Just add the 'void' keyword at the withdraw method and add a get method that returns the balance.
Then create a main method and instantiate the classes: CurrentAccount and/or SavingsAccount and call their methods and print the balance.
Because 'a' and 'b' don't have the same length:
for (int i = a.length-1; i >=0; i--){
sum = a[i] + b[i]+ diff;
}
At the above code you check only if the 'i' will be able to be applied at the 'a' array. But you don't check the length of the 'b' array.
Does the Strings s1,s2 have an attribute named: sort ?
You can not just imagine methods and attributes and call them. The String object doesn't have such thing:
s1.[B]sort[/B];
You need to add an actionlistener to your buttons. Check the api of the JButton and use the addActionListener method. Create a class that implements the ActionListener interface and pass that class as parameter.
In that class you will need to implement only one method:
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source instanceof JButton) {
JButton buttonClicked = (JButton)source;
}
}
The 'source' object is a reference to the component that was clicked. So if you add the above class to other components besides a JButton the 'source' would be that component.
In your case you will add it only to your buttons, so the 'if' wouldn't be necessary.
But I don't know how to move the buttons. Maybe you shouldn't use buttons but something else. Remember you don't have to use buttons. You can use JLabels. You can add a MouseListener in a similar way to the labels and implement the MouseListener interface.
When the mouse is clicked the appropriate method of the MouseListener will be automatically called. And you can change the style of the label to make look like it was moved.
Check the APIs for the above classes
Inside the cases of the do-while you set goOn to false. So you will exit the while-loop:
..
case 1:
myAccount.deposit();
goOn = false;
break;
..
} while (goOn == true);
If you want them completely removed why don't you try the replaceAll method of the String class
Since you are using loops with arrays, read how to get and set the values to each element of the array. Look at your notes or search a tutorial and you will find that what I wrote is one way to declare an array.
The loop the original array calculate the result for each of its elements and put it in the new array.
u shuld do ur homework urslef atleast 1st try it n move ur lazy ***,
You are right not to want to do simplyflawless homework, but your tone doesn't have to be that offensive. Also try to use proper English and not use "bad" language since it is the forum's rules. It doesn't look nice to ask others to follow the rules and not do it yourself.
To simplyflawless
if anyone could help me, i would really appreciate it. please.
ill do anything for whoever helps me.
What do you mean by that? Are you going to give them your soul or offer something else in return which is against the forum rules.
ill help someone with their problem
How will you be able to do that? The one-eyed guides the blind?
You have been given the method signature and the formula. For the power: ^ use the Math.pow() method found at Math class. Check the API.
For reading from keyboard use the Scanner class: Search this forum for examples.
And at least write some code and post it. Start with the method.
Your example convinced me that the valueOf method completes faster; I'm not arguing with you on that. The linear time thing I was talking about is true, but isn't really that important - the main point I wanted to make is that in most cases, the choice of either valueOf or using the '+' operator is really up to the programmer, since either choice will not have a negative effect on the program's run time, as your program demonstrated. (It only took a couple hundred ms for a million operations... the average Java program probably does only a handful of String concatenations, with some exceptions)
You are right, it is up to the programmer. Doing this: "" + 5
is faster to type and easy to read, even though I use the String.valueOf method.
For programs with many concatenations everybody uses the StringBuffer/StringBuilder, so the choice between the '+' or the String.valueOf isn't an issue, because it is not used.
For a single conversion I think that the String.valueOf method is more "formal".
Does this print:
System.out.print("Please enter any string: ");
It should be printed at the bottom of the screen. Enter input there.
The code seems fine. If you run it from command prompt you should be fine.
You haven't defined "reader" also look at the api of the String and StringBuffer class to find the methods you need and ask specific questions.
Also you will need a condition to exit the loop.
In both cases, the String is still created in O(n) time, so the choice of which to use is unimportant. The only time when a developer should bother with something like this is when using one method over another becomes a bottleneck, which in almost all cases, it will not.
Oh, and when I say O(n) here I'm referring to the fact that the operation's completion time is dependent on the number of characters that need to be copied, I'm not referring to the N value from your program.
I wrote that piece of code out of curiosity. Why when I execute it the String.valueOf appears to be faster?