Change Variable Types Accordingly

Mar 14, 2014

GoodEmployee is defined who has ALL the following properties:

He should be married.
He should have 2 or less than 2 children.
His middle name should start with "k" but not end with "e"
The last name should have more than 4 characters
The character "a" should appear in the last name at least two times.
The name of one of his children should be "Raja"

Write a method:

boolean isGoodEmployee( boolean isMarried, int noOfChild , String middleName , String lastName , String[] childNames);

isMarried true if the employee is married.
noOfChild the number of children of the employee.
middleName the middle name of the employee
lastName the last name of the employee.
childNames the array of the names of the children of the employee

View Replies


ADVERTISEMENT

Unable To Change Value Of Instance Variable

May 23, 2014

Is there anything wrong on my code?

import java.util.*;
public class Card
{
private String description;
private String suit;
private int value;
private static final String [] cardName {"Ace","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten",
"Jace","Queen","King","Joker"};

[Code]...

View Replies View Related

All Instances Of Same Class Change Each Other Private Variable

May 2, 2015

I have a class named Base and a private variable named _hopcount i have 10 instances of class base i use _hopcount as creteria to some if but other instances edit _hopcount so i want to prevent _hopcount edit by other instances; I want to have private variable which other instances of same class can't modify it.

public class Base extends TypedAtomicActor {
private int _hopcount = 0;
if(_hopcount <= 3) {
some code;
}
public function() {
_hopCount += 1;
}
}

View Replies View Related

Difference In Variable Assignment Between Constructor And Variable Section

May 21, 2014

Given the case I have an object which is assigned only once. What is the difference in doing:

public class MyClass {
private MyObj obj = new MyObj("Hello world");

private MyClass(){}
//...
}

and

public class MyClass {
private MyObj obj;
private MyClass(){
obj = new MyObj("Hello world");
}
//...
}

Is there a difference in performance or usability? (Given also the case there are no static calls expected) ....

View Replies View Related

Are Terms Local Variable And Member Variable Comparable

Oct 27, 2014

The term "Local variable" is related to scope. That is a local variable is one which is defined in a certain block of code, and its scope is confined inside that block of code.And a "Member variable" is simple an instance variable.

I read in a discussion forum that when local variables are declared (example code below), their name reservation takes place in memory but they are not automatically initialized to anything. On the other hand, when member variables are declared, they are automatically initialized to null by default.

Java Code: public void myFunction () {
int [] myInt; // A local, member variable (because "static" keyword is not there) declared
} mh_sh_highlight_all('java');

So it seems that they are comparing local variables and member variables. While I think a member variable can also be be local in a block of code, isn't it?

View Replies View Related

Reference Variable - Create Another Variable And Set It Equal To First

Jan 11, 2015

Given a reference variable : Car c1 = new Car();

when we create another variable and set it equal to the first : Car c2 = c1;

we're pointing c2 at the same car object that c1 points to (as opposed to pointing c2 at c1, which in turn points at the car). So if we have code like,

Car c1 = new Car();
Car[] cA = {c1, c1, c1, c1};

are we doing the same? Are we creating four *new* reference variables, each of which points at the same car (again as opposed to pointing them at c1 itself)? I think so, but want to make sure I'm understanding this correctly.

View Replies View Related

How To Use Value Of String Variable Cel1 As Variable Name

May 23, 2014

I have a JFrame jf and JPanel jp on it. jp has five TextFields named cel1, cel2.. cel5. I wish to construct a String Cel + for loop index and run a for loop to reset the values of all the text fields using a single statement such as cel1.SetText("abc"). Similar things can be done in foxfro. How does one do it in java?

View Replies View Related

Overloading With Primitive Types?

Jan 22, 2015

Why the following is happening.

For the below code, when I execute it, it prints

Short method 10 //result 1
Sub class short method 10 //result 2

Which is as expected but if I comment out line 3, then it prints

Integer method 10 //result 3
Integer method 10 //result 4

I can understand result 3 is because of an upcast from short to int, since FunWithOverloading will not have a overloaded method with short now. However, what is happening with result 4? Shouldn't it call methodA of the subclass with the argument type short? If its because I have declared the reference variable, derived, of the type FunWithOverloading, then how come the first result correctly picks the overloaded method of the sub class?

class FunWithOverloading{
void methodA(int x){System.out.println("Integer method " + x);}
void methodA(short x){System.out.println("Short method " + x);} //line 3
} class OverloadedSubClass extends FunWithOverloading{
void methodA(short x){System.out.println("Sub class short method " + x);}

[Code] ....

View Replies View Related

Initializing Arrays Of Various Types

Sep 18, 2014

For basic arrays, we can directly combine declaration and initialization as E.g: int[] num= new int[2]

But can we do this do for forming class object arrays? like

class stu
{
.....
}
stu[] s=new stu[2]
????

View Replies View Related

How To Create And Re-use Object Types

Mar 17, 2014

how objects relate to classes and how you can create and re-use object types.on that point, but this has me baffled. I most certainly do not have a firm grasp yet on passing things to and from methods that just makes my head hurt. SO anyway I tried out one of the code examples:

/* ElectricGuitar.java */
class ElectricGuitar {
String brand;
int numOfPickups;
boolean rockStarUsesIt;

[code]...

But I just realized this thing has no main method and only one class defined.....so I guess I just tried to compile.

View Replies View Related

Incompatible Types For Fname

May 6, 2014

import java.util.Scanner;
class potatoes {
public static void main(String[] args){
Scanner user = new Scanner(System.in);
String fname;
fname = user.nextLine;
String newfname = fname.substring(0, 3);
System.out.println(newfname);
}
}

This bit of code isnt working, it has something to do with incompatible types for fname

View Replies View Related

Generic Types In Java

Apr 1, 2014

I am trying to understand the concept of Generics in java. In the introduction to Generic Types, this example is given:

Java Code: public class Box {
private Object object;
public void set(Object object) { this.object = object; }
public Object get() { return object; }
} mh_sh_highlight_all('java'); "Since

Since its methods accept or return an Object, you are free to pass in whatever you want, provided that it is not one of the primitive types." - I understand this.But then it has been changed to a generic class:

Java Code: /**
* Generic version of the Box class.
* @param <T> the type of the value being boxed
*/
public class Box<T> {
// T stands for "Type"
private T t;

public void set(T t) { this.t = t; }
public T get() { return t; }
} mh_sh_highlight_all('java'); "

As you can see, all occurrences of Object are replaced by T. A type variable can be any non-primitive type you specify: any class type, any interface type, any array type, or even another type variable."We can use any type in place of an Object, because Object is a superclass of all classes. But T (or any other class) is not a superclass of all classes. So how do we justify any class being used in place of a random T or any other class?

View Replies View Related

Wrappers And Primitive Types

Feb 23, 2015

I've got a question to ask.

public class AutoBoxingExample {
public void add(Integer intVal){
System.out.println("Wrapper");
}
public void add(int value){
System.out.println("Primitive");
}
public static void main(String[] args) {
AutoBoxingExample auto = new AutoBoxingExample();
auto.add(12);
}
}

The output is "Wrapper". What would be the reason behind it?

View Replies View Related

Incompatible Types / Void Cannot Be Converted To Int

Jul 29, 2014

package input;

import javax.swing.JOptionPane;
public class Input {
public static void main(String[] args) {
int user;
user = JOptionPane.showMessageDialog(null, "Enter Your Age""); ERROR IS HERE
if(user <= 18) {
JOptionPane.showMessageDialog(null, "User is legit");
} else {
JOptionPane.showMessageDialog(null, "User is not legit");
}
}
}

I'm getting this error message :

incompatible types: void cannot be converted to int

unclosed string literal

View Replies View Related

Constructor In A Class Cannot Be Applied To Given Types

Feb 13, 2015

This code works

public class RedShapeDecorator extends ShapeDecorator {

public RedShapeDecorator(Shape decoratedShape) {
super(decoratedShape);
}

Below results in an error

public class RedShapeDecorator extends ShapeDecorator {

protected Shape decoratedShape;
public RedShapeDecorator(Shape decoratedShape) {

this. decoratedShape=decoratedShape;

}

So I am guessing that if you extend class, you should use super to pass objects?

View Replies View Related

Incompatible Types Integer Boolean?

Mar 19, 2014

I am trying to write a program to calculate the monthly amount paid for a mortgage and the total amount but i keep getting this errors Attachment 5998

This is my program:

import static java.lang.Math.pow;
import java.io.*;
import java.util.*;
class MyInput
{ static private StringTokenizer stok;
static private BufferedReader br
= new BufferedReader(new InputStreamReader (System.in));
public static int readInt()

[code]....

View Replies View Related

Why Enum Types Not Being Recognized By Methods

Mar 17, 2014

Attempting to write a custom comparator for sorting songs, but I keep getting errors saying that the types cannot be resolved.

Java Code:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.*;
public class SongCollection {

[Code] .....

View Replies View Related

Value Types - Actual And Formal Parameters

Jun 3, 2014

public static void main(String[] args) {
System.out.println("VALUE Types");
int i = 5;
int j = 11;
System.out.println("i = " + i + ", j = " + j);

[Code] ....

Questions based on the code:

1. Identify all of the actual parameters in the code provided.
(Is it just i, j iArray and jArray)???

2. Identify all of the formal parameters in the code provided.

3. What is displayed in the third println statement?
i = 11, j = 2001

4. What is displayed in the fifth println statement?
iArray = 6 7 2001 9 10, jArray = 6 7 2001 9 10

View Replies View Related

Comparable Interface For Primitive Types

Apr 16, 2014

Why is it necessary to implement the comparable interface for primitive types and not for classes such as Integer, String etc . . . ?

View Replies View Related

2D-dimensional Arrays Of Object Types

Dec 8, 2014

I need to write a class that represents and image of RGB pixels. This class uses a two-dimensional array of object type that represents R,G and B values.

When I compile the class it completes successfully. However, when I try running a "tester" class , I'm encountering errors in a few methods. I've been trying it out for 2 days now, but unsuccessfully thus far..Those method are : rotating the image by 90 degrees clockwise (and vice versa , but I just use clockwise method 3 times for that) , shifting the image sideways, and shifting the image up/down..

I've attached the whole source code for the class , and the list of errors I get when I try to run my tester. URL...

View Replies View Related

New ArrayList Java Collection Types

Jun 26, 2014

I am looking at a snippet of code in my "learning Java 4th edition" by Orielly and there is a small snipped of code which says:

Java Code: Date date = new Date();
List list = new ArrayList();
list.add( date );
..

Date firstElement = (Date)list.get(0); // Is the cast correct? Maybe. mh_sh_highlight_all('java'); so I am typing the same thing in my compiler in a small Driver class and for some reason I have an error and Im dumbfounded...

View Replies View Related

Method Values For Enum Types

Nov 21, 2014

What class does method Planet.values() in the code below belong to? I thought it belongs to java.lang.Enum but when I could not see it in Java API 7.

package enumeration;
public class EnumTest {
public static void main(String[] args) {
//Planet myPlanet = Planet.EARTH;
// Check arguments supplied
if (args.length != 1) {
System.err.println("Usage: java EnumTest <earth_weight>");
System.exit(-1);

[code]....

View Replies View Related

Netbeans - Method Cannot Be Applied To Given Types

Sep 23, 2014

Here is my code:

import java.util.*;public class DebugSix {
public static void main(String[] args) {

ArrayList<String>products = new ArrayList();
products.add("shampoo");
products.add("moisturizer");
products.add("conditioner");
Collections.sort(products);

[Code] ....

I am using netbeans and getting errors for display(); and size(); it is telling me the errors are :

for the display error, "method display in class DebugSix cannot be applied to given types;
display();" and for the size() is : "cannot find symbol System.out.println("
The size of the list is " + size());"

View Replies View Related

How Many Types Of Inputting Method In Java

Jun 30, 2014

how many types of inputting method in java?can you explain with examples?

View Replies View Related

Incompatible Types - Int Cannot Be Converted To Boolean

Mar 12, 2015

int a, b, sum;
System.out.print("the sum is");
Scanner in = new Scanner(System.in);
a = in.nextInt();
b = in.nextInt();
Sum = a + b;
System.out.println("the sum us" + sum);

in "sum = a+b" its says "incompatible types: int cannot be converted to boolean" I dont understand why

View Replies View Related

Bad Operand Types For Addition Operator

Apr 25, 2015

I would like to know that i am getting an error with the random. it is say bad operand types for binary operator ' +'

[/ Random generator = new Random(15) + 1];

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved