How Is Class Cast In Method Parameter
Jan 15, 2014
how is cast a class at a methods parameters? i have a problems in a methods paramter. i draw red line my exception and mymethods is orage color ....
Caffe drink = new Caffe();
CoffeCup cup = new CoffeCup();
cup.setTempeture(100);
drink.drinkcaffe((CaffeCup)(cup.getTempeture()));
[code]....
View Replies
ADVERTISEMENT
Feb 19, 2014
how to get an access to the method with a parameter of class type variable, lets say: public void insert(Record newRecord, int pos)?
View Replies
View Related
May 21, 2014
import java.util.ArrayList;
public class Demo {
class Expr {}
abstract class Factory <T extends Expr> {
abstract T generate();
[Code] ....
Doesn't Factory2 produce Statements?
View Replies
View Related
Jun 5, 2014
I'm developing an application to track the status of a production flow-line and have hit a bit of a snag. When attempting to read saved data I run into this:
Exception in thread "main" java.lang.ClassCastException: flowline.End_Of_File cannot be cast to flowline.Operation
at flowline.Station.checkLoadPreviousStationStatus(Station.java:91)
at flowline.Station.main(Station.java:212)
Java Result: 1
I've been reading up on different methods to saving and retrieving data and have decided ObjectInputStream would be the best option.
The save method works fine, I opted to use a EndOfFile class to determine when I've reached the end of the input stream. The problem is, when my loop encounters this object, it doesn't terminate the loop.
public void checkLoadPreviousStationStatus() throws FileNotFoundException, IOException,
ClassNotFoundException, EOFException, TempArrayOutOfBoundsException{
Object loadOpn = null;
End_Of_File eof = new End_Of_File();
File f = new File(fileName);
[Code] .....
The Operation cast is a cast to the objects my LinkedList contains. The highlighted line is where the exception occurs.
View Replies
View Related
Jul 20, 2014
I was following a tutorial for libGdx and suddenly became confused by the following syntax:
up.addListener(new ClickListener(){
public void clicked(InputEvent event, float x, float y){
position.y += 1f;
//currentFrame = animation.getKeyFrame(12);
}
});
"up" is basically a text Button:
TextButton up = new TextButton("up", textButtonStyle);
and .addListener is just one of the methods "TextButton" has (actually I think its inherited from "Button" but that doesn't matter).
Basically my question is what's going on inside the parentheses? From what I see its a new instance of "ClickListener" but then suddenly they override an actual method within. Is this simply just a way to override a method from the ClickListener class or is it something else?
View Replies
View Related
Jan 2, 2014
So, I am learning Swing, and Swing Layouts. I just came across a really confusing (for me) line of code.
Java Code:
setLayout(new BorderLayout()); mh_sh_highlight_all('java');
So, this is weird for me because I don't really understand why the BorderLayout class constructor is being initialized as a parameter for the setLayout..
View Replies
View Related
Jan 30, 2015
Java SE Runtime Environment build 1.8.0..This is part of the code:
public static int addAddress (String[] number, boolean[] front, double[] total) {
int num = 0;
double ffee = 0;
/*boolean value = false;*/
[code]...
I have tried using the line of code commented out, /*boolean value = false;*/. However, another error is displayed. The compiler shows the following...
Inspection.java:33: error: incompatible types: boolean cannot be converted to boolean[]
front[num]= defineFront(num, value);
^
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output error...I know that boolean values are by default stored as false, once you create the array. However, I'm having trouble passing the variable to the method.
View Replies
View Related
Jan 3, 2015
I have a method that accepts as a parameter an object:
public void addClient(Client c){ }
Client is a class which has a constructor that has two String parameters:
public Cliente (String name, String lastname){
this.name = name;
this.lastname = lastname;
}
In the main add a Client in this way:
addClient(new Cliente("first", "second"));
Now, i have an array of Client, so I would like to enter within this. Example:
public void addClient(Client c){
for (int i = 0; i<client.length ; i++) { // client is an array of Client object
client[i] = c; // Enter a c in the array, but does not work!
System.out.println("test "+clienti[i]); // print Client@15db9743
}
}
I have used the println for check if worked insertion, but the result shows no
View Replies
View Related
Jul 1, 2013
I was going through some lectures online and found that to compare or even swap, the use of comparable or comparator argument like
public static boolean less(Comparable v,Comparable w)
{
return v.compareTo(w)<0;
}
public static void swap(Comparable []a,int i,int j)
{
Comparable swap=a[i];
a[i]=a[j];
a[j]=swap;
}
I did not get the use of passing Comparable or Comparator to the function as parameters. Object as parameter could have been used too?
View Replies
View Related
Jul 31, 2014
I have the following method:
public static void doSomething(List<? extends GenericClass> input)
{
// op
}
^
This compiles and works, ensuring I can only pass in a List of a type extending GenericClass.But now I want it to accept an Array instead of List. This is where I'm stuck:
public static void doSomething(<? extends GenericClass>[] input)
{
// op
}
^
A wrong guess at the syntax which does not compile. One solution is to convert Array into ArrayList before calling the 1st method above, but I'd rather bypass this if possible.
View Replies
View Related
Apr 28, 2015
I have a drop-down which contains the four sections simple buttons(filters). When click any of these buttons some settings are applied. I have successfully auotmated it using simple if else and switch but in that case i have to use 8 parameters(8 are the number of button)
public void editFilters(WebElement filter1, WebElement filter2, WebElement filter3, WebElement filter4,WebElement filter5,WebElement filter6,WebElement filter7,WebElement filter8 String edit, String expectedColour) {
switch (edit) {
case "selectFilter":
if (filter1 != null) {
[Code] .....
But want to make it more effective by using hashes. I do not want to use 8 different parameters to perform action on the respective button.
So now what i want to implement.
Create a method in which i pass the parameter1 as hash and 2nd parameter as 0 or 1, 0 means unSelectFilter and 1 means select the filter.
With parameter 1, in code i want to pass the name or xpath or anything else for any number of filters , that those filters names should be stored into that hash and then by passing 0 or 1, i can select/unselect those filters.
View Replies
View Related
Jul 14, 2014
I am following this article [URL] .... till now I have made some code
This is my Interface
public interface Comparable<T> {
public int compareTo(T o);
}
And this is my class where I am using Bound Type Parameter on Generic Methods
public class GenericMethodBoundType {
public static <T extends Comparable<T>> int countGreaterThan(T[] anArray, T elem) {
int count = 0;
for (T e : anArray)
[Code] .....
What else I need to do both in main method and at what parameterized types I need to pass at the class?
View Replies
View Related
Apr 9, 2014
The instructions:
Write a method named hopscotch that accepts an integer parameter for a number of "hops" and prints a hopscotch board of that many hops. A "hop" is defined as the split into two numbers and then back together again into one.For example, hopscotch(3); should print:
1
2 3
4
5 6
7
8 9
10
how to start this program.
View Replies
View Related
Apr 23, 2014
If you write
byte b = 100; it works (implicit conversion of implicit int literal 100 to byte.
But if you have a methodvoid bla(byte b){}
And want to invoke it with a literal (which is an int by default):bla(8) then there is no implicit conversion.
Is the byte b = 100; just an exception in Java? And is it the rule that one has to explicitely cast (narrow) integer literals when passing to smaller-than-int types?
View Replies
View Related
Mar 9, 2015
so i have this question where it wants me to create a recursion method that takes ONLY THE ARRAY as a parameter, and without using loops or static variables inside the method, and then the method returns the smallest value in that array. However, i tried making the simple if statements where i compare the first element of the array with the second element using the length of the array and decreasing it to get the next elements and compare it again by calling the recursion method, but the problem is when i call the method again, the length does not decrease, even if i store it in a variable, the variable will initialize itself again, and the length wont change.
View Replies
View Related
Feb 7, 2015
In my book for learning java, one of the questions asks us to create a method header named convertTOKM that takes an int parameter, which is the number of miles, and returns a double value for the converted value in kilometers. I made one, but wanted to know if I was right in any way.
Here it is:public double convertTOKM(int miles, double kilometers){
View Replies
View Related
Nov 4, 2014
I'm doing an assignment where a method receives a Queue as a parameter then convert it into a stack, I made it but now I want to try it in my main, but how can I do this? there's no toString and I cant use a for loop because I'm using a queue and not an array. This is the head of my method:
public static void QueueStack(Queue<Integer> q){
View Replies
View Related
May 9, 2014
What is the name given to two (2) or more methods in a class with the same name but different parameter/ argument lists?
View Replies
View Related
Mar 8, 2015
I have a class of Date with a constructor with 3 parameters in it. Those 3 parameters are int data type just to enter month, year, day.
I have another class called Author which has a constructor of Date diedDate; as a parameter passing to the Author constructor.
I was asked to call the Date parameter is null, call the default constructor but I thought for the Date parameter I could only enter something like 0,0,0 instead of typing in null, null, null because null is for String data type isn't it?
View Replies
View Related
Nov 1, 2014
I am practicing some basic recursion and I was trying to solve this problem
Write a method sumTo that accepts an integer parameter n and returns the sum of the first n reciprocals. In other words:
sumTo(n) returns: 1 + 1/2 + 1/3 + 1/4 + ... + 1/n
For example, the call of sumTo(2) should return 1.5. The method should return 0.0 if passed the value 0 and should throw an IllegalArgumentException if passed a value less than 0.
This is my attempt to do it , however my output is always a 0.0 , and i do not understand why :
public static double sumTo(int n ){
if(n<0){
throw new IllegalArgumentException();
}
else if (n==0){
return 0.0;
[Code] .....
View Replies
View Related
Apr 22, 2014
i have to "Write a method called addToOverThirty which takes the array nums3 as a parameter. It adds 1 to all numbers that have a value greater than 30 in the array.
Add a call to the addToOverThirty method, then display a message telling what the output is followed by the results For example:The nums3 array after adding 1 to all numbers greater than 30 is10 6 15 and so on (check with the values you assigned to nums3)"its pointless because we were told not to make the array have a number over 30.
import java.lang.*;
import java.util.*;
public class LastProject
public static void main(String[] args) {
int nums1[] = new int[15];
int nums2[] = new int[15];
int nums3[] = {5,2,15,8,26,22,1,29,4,23,30,11,19,6,24};
[code]....
View Replies
View Related
Mar 4, 2014
Instructions
Create a WordCounter class with a constructor that takes a file name as a parameter
The class should have two fields: one for the file name and one for a HashMap to store word count information
The constructor should call a private method, countWords, that reads in the file and counts the word frequencies
The class should contain a get method for each field, as well as a print method that prints out the map in the following format:word:frequency
When printing, the map should be sorted by either the word order or frequency (Hint: see Collections.sort)
You should include the sample text file on Blackboard. This is what i got so far
Java Code:
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Scanner;
public class WordCounter
[Code] ....
View Replies
View Related
Nov 27, 2014
I have two methods with parameters out of the main method, both of them work fine alone but I am finding a problem to use them together. I need to use the parameter of Method one in the second Method.
View Replies
View Related
Dec 13, 2014
Assuming that we have two classes B and C which inherit from class A. What is the best way to pass a parameter from an object of class B to an object of class C by the use of class A without using static variable and without defining a get function in B?
View Replies
View Related
Nov 18, 2014
I had a TestColor class which contained methods to change hue, saturation, brightness, red, green, blue of TestColor's instances but also had static methods which take in an additional parameter for an instance of TestColor and returns the affected instance of TestColor. Now instead of having one method for every possible color effect to be applied to an image, how can I have one method that takes in an Image parameter, a static or non-static method reference from TestColor parameter and lastly an intensnity value parameter. This is so that I can make an affectedImage object instance inside the method and a Graphics2D object for drawing to each pixel of the new image, now I have one for loop and one nested for loop for the x and y pixels of width and height of the old image and inside the nested for loop I'd create a TestColor by calling getRGB on the image's pixel. Then I would apply the static or non-static method reference somehow to change the color with the intensnity value and after applying it draw to the new Image with Graphics2D. How to would I parametize a method reference and be able to use it in such way?
View Replies
View Related
Feb 4, 2015
I want to know is there any way we can call parent class method using child class object without using super keyword in class B in the following program like we can do in c++ by using scoop resolution operator
class A{
public void hello(){
System.out.println("hello");
}
}
class B extends A{
public void hello(){
//super.hello();
System.out.println("hello1");
[code]....
View Replies
View Related