|
wkid87 wrote: Function f = new LinearFunction();
I don't see a definition for LinearFunction() anywhere.
|
|
|
|
|
Sorry, I'm double post the Function.java code Below is my Linear Function.java.
<br />
<br />
<br />
package Function;<br />
<br />
<br />
public class LinearFunction implements Function{<br />
private double slope = 1.0;<br />
private double yIntercept = 0.0;<br />
<br />
public double getSlope(){<br />
return slope;<br />
}<br />
<br />
public void setSlope(double s){<br />
slope = s;<br />
}<br />
<br />
public double getYIntercept(){<br />
return yIntercept;<br />
}<br />
<br />
public void setYIntercept(double y){<br />
yIntercept = y;<br />
}<br />
public double getValue(double inValue){<br />
return slope+inValue+yIntercept;<br />
}<br />
<br />
}<br />
<br />
I will like for all the methods that are in the LinearFunction.java to load in Main.java. The only methods that is loading up correctly is get.Value. There is an error in my Main.java file using setSlop and getSlope. I'm new to Java and I hope this information help.
|
|
|
|
|
wkid87 wrote: There is an error in my Main.java file using setSlop and getSlope
Well, you need to tell us what the error is; we cannot guess!
|
|
|
|
|
In your Main class you have declared f, g and h as being of type Function. So, although the actual type that you create at runtime is LinearFunction, you can only access the method which is defined on the Function interface (getValue) because that is all the compiler knows about for f, g and h.
If you want to access the methods on LinearFunction which are not on the Function interface, you will need to either declare the variables as being of type LinearFunction, or cast them to LinearFunction so that the compiler knows what it is dealing with at this point:
((LinearFunction) f).getSlope()
You probably want to read up a bit about interfaces in Java and how they work and how to use them. Or, if this is a homework question, ask your tutor to explain it.
|
|
|
|
|
This is the error I'm getting:
C:\Users\John\Documents\NetBeansProjects\Lab 2\src\Function\Main.java:36: cannot find symbol
symbol : method getYIntercept(double)
location: interface Function.Function
f.getYIntercept(1.0);
1 error
But I was able to find getValue without any problems.
modified on Wednesday, September 23, 2009 11:54 PM
|
|
|
|
|
As I said, you have declared f as being of type Function. There is no getYIntercept method on the Function interface, so the compile fails. There is a getValue method on the interface, so that is OK.
You need to read up about interfaces.
There are some other problems with your code, as noted by the other poster.
|
|
|
|
|
1. f,g, and h define them as LinearFunction (as mentioned by David) this will solve the problem of accessing the methods from LinearFuction.
2. In Linear Function you need to create constructors 3 to be specific (one empty,one with one double variable, and one with two double variables). Do note that constructors have same name as class.
3. In f.getYIntercept() you cant set a value in a get method unless you specify it in the class plus thats wrong only set methods should have parameters.
4. In f.setSlope() you cant use it because it does not have a return value to be printed. In addition you are not giving the slope any value to be set. This is in the System.out.println();
Good Luck
|
|
|
|
|
I have read through numerous forums and sites, but i couldn't find exactly what i was looking for.
I am trying to just write a Java program for a single solution to the "N-Queens Problem" also known as the 8 queens problem. i know what i want to do i just dont know how to put it into code.
import java.io.*;
import java.util.*;
public class NQueensProblemSingleSolution {
private int n;
private int[] solutionVector;
public NQueensProblemSingleSolution(int n) {
if (n % 6 == 2) {
for (m=1; m++; m=n/2)
}
if (n % 6 == 0) {
for (m=1; m++; m=n/2)
}
if (n % 2 != 0) {
for (m=1; m++; m=n/2)
}
}
@Override
public String toString() {
return "This is the solution: "+solutionVector;
}
public static void main(String[] args) {
if (args.length != 1) {
System.out.println("usage: java NQueensProblemSingleSolution <outputfile>");
System.exit(0);
}
try {
PrintWriter outFile = new PrintWriter(new FileWriter(args[0]));
for (int i = 4; i <= 16; i++) {
NQueensProblemSingleSolution result = new NQueensProblemSingleSolution(i);
outFile.println(i + ": " + result);
}
outFile.close();
}
catch(FileNotFoundException fnfe) {
System.out.println("Open error: " + fnfe);
}
catch(IOException ioe) {
System.out.println("I/O error: " + ioe);
}
}
}
okay some notes i forgot to mention before:
n queens are being arranged on an n x n chessboard.
so, if the chess board is 8 by 8 then there should be 8 queens.
i have tried to make a two dimensional array setting it with rows and columns but i havent been able to get it right because it wont work i might be going the wrong direction.
for example i tried this:
public NQueensProblemSingleSolution(int n) {
int rows;
int columns;
Array[][] one = new Array[][];
if (n % 6 == 2) {
for (m=1; m++; m=n/2)
board[n][n]= ;
}
i knew this wouldnt work and i just threw it together quickly to try and get somewhere but i declared board as a String[][] in the top.
i also forgot to mention the main section with args[] isnt something i need to change, i just included it so that it would be more clear what i am trying to use the code for.
please tell me if i forgot to mention anything else that would help make it more clear to show what i am looking for
i think we are supposed to just add mroe to the public NQueensProblemSingleSolution(int n) and i feel like we have to do more than just that i dont know
any help or hints at all would be greatly appreciated
sorry for including so much code
another note: the if and for loop sections are for the algorithm itself, depending on the 3 conditions we were given that 1. n is even but not of the form 6k + 2 (if n%6 == 2). 2. n is even but not of the form 6k (n%6==0) and 3. n is odd (n%2 != 0)
(i came up with the n% parts so if they are wrong tell me i think they are right though :z)
|
|
|
|
|
|
right, i have read the algorithm from there, but the place i got the one im using is from our sheet of paper the professor gave us.
and i have been to that page that you sent me in the second link
i see, i suppose, how to do it
but is there a way to do it without having to make enumarate twice, printQueens, and isConsistent?
i am asking if there is a way i can do it in just one public NQueensProblemSingleSolution(int n)
and then a toString() method,
and also i have come across a new discovery since i posted this i have tried:
if (n % 6 == 2) {
for (int m=1; m<=n/2; m++) {
for(int k = 0; k<2; k++) {
if(k == 0) {
j = 1 + (2*(m-1) + n/2 - 1) % n;
i = m;
}
else {
j = n - (2*(m-1) + n/2 - 1) % n;
i = n + 1 - m;
}
solutionVector[--j] = --i;
}
}
}
(this is for the case where n is even but not of the form 6k + 2)
and i declared int i, j above now.
but i am sort of confused with this, would it be right?
i mean
ugh i cant ask it correctly,
|
|
|
|
|
Hi,
I'm reading a .jsp page to a Outfile. I need to search for Text inside the Outfile so that I can replace the text with something else.
Does anyone have an idea how to do this?
Thanks
Mbu
|
|
|
|
|
import java.io.*;
import java.util.*;
public class ReadAndChange
{
public static void main(String[] args) throws Exception
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter file Name:");
File theFile = new File(scan.next());
Scanner scan2 = new Scanner(theFile);
PrintWriter out = new PrintWriter(new File("out.format"));
while(scan2.hasNext())
{
String q = scan2.nextLine();
if (q.equalsIgnoreCase(""))
{
out.println("The result of manipulation");
}
else
out.println(q);
}
out.close();
}
}
Hope this helps
|
|
|
|
|
Hi All,
I am having problem getting values from jinternalframe, what i am doing is i got a JFrame that opens multiple JInternalFrame with JTextfield in it, now i want to get all the JTextfield value in the JInternalFrame, i need the function to be in the JFrame, is this possible, if not any recommendation is appreciated.
Thanks.
Stephen Michael
---------------------
www.islasolutions.net
|
|
|
|
|
In this code I show you the way to get the value for one all you need to do next is a simple loop to get the other values. This can be done by keeping a counter for the number of internal frames you have (make sure the JTextFields have consistent naming) then use a for loop to concatenate the values and display them in the label.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class InternalFrameExample extends JFrame
{
JLabel l;
JInternalFrame g;
JTextField t;
JButton b;
public InternalFrameExample()
{
g = new JInternalFrame();
t = new JTextField();
g.add(t, BorderLayout.NORTH);
g.setVisible(true);
g.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
l = new JLabel();
b = new JButton("poke");
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
l.setText(t.getText());
}
});
add(l, BorderLayout.NORTH);
add(b, BorderLayout.SOUTH);
add(g, BorderLayout.CENTER);
setTitle("InternalFrameExample");
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 400);
}
public static void main(String[] args)
{
new InternalFrameExample();
}
}
Hope this helps
|
|
|
|
|
Hey guys,
I am working on an assignment that concerns itself with recursion. My program compiles, but does not run properly. The program is similar to checking that a string is a palindrome, but it is a little different. This program is supposed to check that each character in the string has a correct corresponding character. For example, in this assignment A's pair with T's and C's pair with G's. So the input AATT should be evaluated as a biopalindrome. My program runs correctly if I input a string that does not contain any A's, T's, C's or G's, but if I input the above expression, AATT, the error message I receive when the program runs is this:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 4
at java.lang.String.charAt(String.java:558)
at bioPalindrome.checkPalindrome(bioPalindrome.java:42)
at bioPalindrome.checkPalindrome(bioPalindrome.java:45)
at bioPalindrome.checkPalindrome(bioPalindrome.java:45)
at bioPalindrome.checkPalindrome(bioPalindrome.java:45)
at bioPalindrome.checkPalindrome(bioPalindrome.java:45)
at bioPalindrome.main(bioPalindrome.java:24)
Here is the code that I have so far:
import java.util.Scanner;
public class bioPalindrome {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.println("Please enter the DNA string to be examined.");
String dnaString = console.nextLine();
dnaString = dnaString.toUpperCase();
System.out.println(dnaString + " " + dnaString.length());
boolean isPalindrome = checkPalindrome(dnaString, 0, dnaString.length());
if(isPalindrome == true)
System.out.println("The DNA string entered is a biopalindrome.");
else
System.out.println("The DNA string entered is not a biopalindrome.");
}
public static boolean checkPalindrome(String stringIn, int start, int end) {
if(stringIn.length() - 1 == 0 && start == end)
return true;
else if(stringIn.charAt(start) == 'A' && stringIn.charAt(end - 1) == 'T' || stringIn.charAt(start) == 'T' && stringIn.charAt(end - 1) == 'A' || stringIn.charAt(start) == 'C' && stringIn.charAt(end - 1) == 'G' || stringIn.charAt(start) == 'G' && stringIn.charAt(end - 1) == 'C') {
start++;
end--;
return checkPalindrome(stringIn, start, end);
} else
return false;
}
}
I appreciate any help or suggestions anyone can offer. Thanks again for your help.
|
|
|
|
|
Hey guys,
I made a small change and the code seems to be working properly now. The revision I made is that in the user-defined method, checkPalindrome, I changed the first conditional statement to if(start == end). So instead of
if(stringIn.length() - 1 == 0 && start == end)
The code now reads
if( start == end)
I'll try some more combinations and if I find an error, I'll post back.
|
|
|
|
|
Does Code Project have ongoing projects which one can participate in like a spectator or also join the coding team?
Is it possible to download project source code from a project on code project.
Priety.
|
|
|
|
|
prietycool wrote: Is it possible to download project source code from a project on code project.
Yes, see these articles[^].
|
|
|
|
|
Hi,
I want to participate in a java web project which uses springs and hibernate.
So that I can learn to configure and use springs and hibernate in a web project.
Is this possible on code project?
Priety.
|
|
|
|
|
|
We have to use different sorting algorithms to sort an array. So I already got the quick, bubble, and insertion sort working and I am not posting the code since it already works However the Merge Sort is giving me problems. I already searched online but could not find the right answer. Anyway...here it goes. We were provided with some code snippets and have to make them work. What data do they want when they say min, max? I thought that it would be the first element in the array and the last....No? cause its not working like that. Here is my code please someone help me what they want...
import java.io.*;
import java.util.*;
class MergeSortArray
{
public static void main(String[] args)
{
int choice = 0;
int[]values;
int counter = 0;
int backCounter = 10;
values = new int[10];
while(counter != 10)
{
Scanner scanner = new Scanner (System.in);
System.out.println("Enter " + backCounter + " more integers:");
choice = scanner.nextInt();
values[counter] = choice;
counter++;
backCounter--;
}
System.out.println(counter);
mergeSort (values, 0, values.length-1);
merge (values, first, mid, last);
int i=0;
while(i<10)
{
System.out.println(values[i]);
i++;
}
}
public static void mergeSort (Comparable[] data, int min, int max)
{
if (min < max)
{
int mid = (min + max) / 2;
mergeSort(data, min, mid);
mergeSort(data, mid+1, max);
merge (data, min, mid, max);
}
}
public static void merge (Comparable[] data, int first, int mid, int last)
{
Comparable[] temp = new Comparable[data.length];
int first1 = first, last1 = mid;
int first2 = mid + 1, last2 = last;
int index = first1;
while (first1 <= last1 && first2 <= last2)
{
if (data[first1].compareTo(data[first2]) < 0)
{
temp[index] = data[first1];
first1++;
}
else
{
temp[index] = data[first2];
first2++;
}
index++;
}
while (first1 <= last1)
{
temp[index] = data[first1];
first1++;
index++;
}
while (first2 <= last2)
{
temp[index] = data[first2];
first2++;
index++;
}
for (index = first; index <= last; index++)
data[index] = temp[index];
}
}
Please help me to get this working. Pretty Please!!!
|
|
|
|
|
1. Did you revise what your teacher said about merge sort.
2. Min is the smallest value in the array and Max is the biggest value in the array.
I did not check your code but here is a nice working example:
http://www.cs.colorado.edu/~main/applications/Mergesort.java[^]
In line 42 modify it to mergesort(data, 0, data.length);
Good luck
|
|
|
|
|
Member 4277480 wrote: In line 42 modify it to mergesort(data, 0, data.length);
Thanks for the answer. But from what I can see this is pretty much what I have.
mergeSort (values, 0, values.length-1);
Here is the error message I am getting maybe this will help
MergeSortArray.java:30: mergeSort(java.lang.Comparable[],int,int) in MergeSortArray cannot be applied to (int[],int,int)
|
|
|
|
|
Doh got it fixed had to change comparable [] to int[] ...silly mistake.
|
|
|
|
|
By the way the line I told you to change was from the link I told you about not yours
Any way good to hear you got it to work.
Regards
|
|
|
|