Declaring Parameters - Use Void For

Mar 1, 2015

I want to have parameters that I use the "void" for, in other words it doesn't return anything.

class code
{
void go()
{
int TestStuff t = new TestStuff();
t.takeTwo(12,34)
}
void takeTwo (int x, int y) {
int z = x + y;
System.out.println("Total is:" + z);
}
}

View Replies


ADVERTISEMENT

Declaring Parameters (fields As Protected String) In Java Class

May 19, 2014

I am trying to declare fields as protected String custom.field.1096; in my java class but it does not allow me. Can I not declare the field as above? Is there any workaround to achieve this?

View Replies View Related

Declaring Object Using Interface Implemented By Its Class Vs Declaring Object Using Class

Apr 8, 2014

Suppose you have a generic Dog class of the pet type that implements a DogInterface of the pet type. What is the difference between;

DogInterface<pet> Rex = new Dog<pet>();

and

Dog<pet> Tye = new Dog<pet>();

In what situations might you want to use Rex instead of Tye?

View Replies View Related

What Is Different Between Void Main And Public Void Main

Sep 15, 2014

what is different between void main() and public void main()

View Replies View Related

Declaring Variable And Using Getter

Jul 2, 2014

I have question on best practice on declaring variable and using getter. Is there any performance issue if I used getter every time to access the properties values or Is better to use getter once, store in variable then use that variable whenever needed.

a) What is the best practice?

b) Also what if getter in deep level e.g. myTopObj.getInnerOne().getInnerTwo().getProp();

Option 1

Java Code:
var myProp = obj.getProp();
x = myProp;
y = myProp mh_sh_highlight_all('java');
Option 2
Java Code: x = obj.getProp();
y = obj.getProp(); mh_sh_highlight_all('java');

View Replies View Related

Declaring Variables In GUIs

Aug 25, 2014

In most of the GUI examples, it declares the variables right at the start as private.

public class KiloConverterWindow extends JFrame
{
private JPanel panel;
private JLabel messageLabel;
private JTextField kiloTextField;
private JButton calcButton;
private final int WINDOW_WIDTH = 310;
private final int WINDOW_HEIGHT = 100;

In one example though, it declares and creates the different objects in the constructor.

public BorderWindow()
{
setTitle("Border Layout");
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());

JButton button1 = new JButton("North Button");
JButton button2 = new JButton("South Button");
JButton button3 = new JButton("East Button");
JButton button4 = new JButton("West Button");
JButton button5 = new JButton("Center Button");

Now what I assume, is that for the second example snippet, because it's sole purpose is to show you the Border layout, there are no events tied to any buttons, and there is no data, other than the names of the buttons. With the first snippet, it's purpose was to show the kilometer to miles converter using a GUI. The purpose of making it private is to prevent the data from being altered from outside code. If I have the right idea, I feel like they should have continued to keep their examples consistent.

View Replies View Related

What Is The Impact Of Declaring A Method As Final

Nov 14, 2014

1 Can method declared as final be overridden and overloading ?

2 if A is static sub class of B. how to access a method in class A ?

View Replies View Related

Table Booking In Restaurant - Declaring 2D Array

Oct 29, 2014

import java.io.*;
public class Booking {
private Customer[] [] booking = new Customer [7] [tables];
private int count = tables;
private String restaurant;

[Code] ......

View Replies View Related

Declaring Static Int - Illegal Modifier For Parameter

Feb 15, 2014

I accidentally wrote a code differently than what I should've, and I got these errors :

"Illegal modifier for parameter a; only final is permitted"
"Illegal modifier for parameter b; only final is permitted"
"Illegal modifier for parameter c; only final is permitted"

The code that I wrote and gave these errors:

Java Code:

class Math {
public static void main(String[] args) {
static int a = 11;
static int b = 35;
static int c = 29;
//the rest of the code below

[Code] ....

I noticed that I can declare "static int" only under "class Math" and not under "public static void main".(I had to remove "static" if declaring int under "public static void main");

View Replies View Related

Method Implementation Declaring Exception Other Than That Declared In Interface

Aug 31, 2014

The 2 minute drill from page 69 SCJP kathy and bert book, says regarding Interfaces, that - "A legal nonabstract implementing class must not declare any new checked exceptions for an implementation method."

When I try the below given code in eclipse , it does not throw any errors . (Here I have tried to throw NullPointerException from testFunc whereas the interface function throws IllegalStateExc)

package abstracttesting;
public class StaticCheck implements check{
public void testFunc() throws NullPointerException{
// TODO Auto-generated method stub
} public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
interface check{
void testFunc() throws IllegalStateException;
}

View Replies View Related

Declaring Enums - Could Not Find Or Load Main Class

Feb 25, 2014

I've just started, so right now I'm reading about declaring enums, the book lists the following code

enum CoffeeSize {
//8,10 & 16 are passed to the constuctor
BIG(6), HUGE(10), OVERWHELMING(16);
CoffeeSize(int ounces){ //constructor
this.ounces = ounces;

[Code] .....

I'm assuming that code to be in a same file since enums can be declared within and outside a class, so I saved it into a file named "Coffee.java", it compiles just fine from command line but when I try to execute "java Coffee" it throws "Error: Could not find or load main class Coffee"...

View Replies View Related

When To And Not To Use Void

Apr 9, 2014

I know void means it doesn't return anything but to me when I think it doesn't return anything it just does equations and other things. So why can you use this? Or is Void like returning input such as a Scanner?

void printStates() {
System.out.println("cadence:" +
cadence + " speed:" +
speed + " gear:" + gear);
}

View Replies View Related

Using String In A Void Or INT?

Apr 20, 2014

I am trying to make a custom texture system for a block in Minecraft, I am not too advanced with Java and am not sure how to make this work the way I want it to.

Java Code:

/** The list of the types of step blocks. */
public static final String[] blockStepTypes = new String[] {"stone", "sand", "wood", "cobble", "brick", "smoothStoneBrick", "netherBrick", "quartz"};
private Icon missing;
private Icon icon1;
/**
* From the specified side and block metadata retrieves the blocks texture. Args: side, metadata
*/

[code]....

Alright, so basically I figured I could just tell the code to see if the block is made out of Stone, then to set the texture to Stone, or if it's made out of Sand, then set it to Sand.What I usually get is Eclipse telling me to "insert '!= null' check", "insert '!= null' check", and then just error out saying "Opperator != is undefined for the argument type(s) boolean null"

View Replies View Related

Working Through Printf And Void

Sep 1, 2014

I have a program that I have created from scratch called Product and I also created a Tester program to test my theory. The only problem that I have is that I am getting an error with this statement System.out.printf statement. The statement needs to print out "Program [name = HP ENVY x360 TouchSmart, price = $xxxxxx". The error states that "The method printf(String, Object...) in the PrintStream is not applicable for the arguments (String, String, void).

Which tells me that it is referencing back to the class file where I have

public void applyDiscount(double percent) {
percent = price - (price * percent); //establishing the discount
}

The lines of code in my tester file are (partly displayed)

String formatString = "Product[name = %s, price = %.2f]";
System.out.printf(formatString, product.getName(), product.getPrice())
//user input the discount amount
System.out.println("Enter discount percentage [0 - 100]: %.2f ");
double discount = user_input.nextDouble();
System.out.printf(formatString, product.getName(), product.applyDiscount(discount)); //This is the line giving me an error.
}

View Replies View Related

How To Assert A Void Method

Nov 5, 2014

how to assert a void method like shown in the photos,

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

Void And Static In Main Method?

Jan 7, 2014

What does void and static mean in the main method?

View Replies View Related

Invoke A Void Method From Another Class?

May 31, 2014

I'm pretty new to Java. I was working the project about gamble. I'm having trouble with invoking the void method from another class. This is what I have done so far.

There are two classes, and I'm trying to invoke gambleAnotherRound method from gambler class into highlighted part in casino class. So, what I want to do is when the program generates "else" part, it goes back into another gamble round.

public class gambler {
public void gambleAnotherRound(double dollarsBet) {
dollarsSpent += dollarsBet;
Random randomNumbers = new Random();
double randomDouble = randomNumbers.nextDouble();

[Code] ....

Below one is casino class.

// use a System.out.print statement to ask how much money each gambler should bet.
// Then declare a variable dollarsBet of type double, and set its value to the keyboard's
// keystroke (be sure to use keyboard.nextDouble()).
// If the user enters 0, then issue a break statement, so that the while loop terminates
// Else, invoke the gambleAnotherRound method of each Gambler, and pass it the variable dollarsBet

[code] ....

View Replies View Related

Variable Is Set Works / But Then In Void - Returns 0

Jun 13, 2014

The id variable is the problem Java Code: package com.cjburkey.games.boxee.objects;

import java.awt.Graphics;
import java.awt.Rectangle;
import com.cjburkey.games.boxee.GameState;
import com.cjburkey.games.boxee.resources.Images;
public class Block extends Rectangle {

[code]...

In the constructor, it returns corrent numbers, in the draw method, it returns 0. Why?

View Replies View Related

Call Void Method For Array

Mar 6, 2014

I am writing a program to take user input in order to create an array, then call a void method that will read in the numbers (from user's input) and fill the array.This method must use a loop to do this.(The array is to be passed to the void method as a parameter)

in theory, this should change the contents of the array, but without returning a result. Because it is a void method, the array is only passed through the method, not returned; Am I correct?How can I return the array and display it without having to change my method type?

Here is my code:

Java Code: package program7array;
import javax.swing.JOptionPane;

public class Program7Array
public static void main(String[] args) { // main method
int howMany = Integer.parseInt(JOptionPane.showInputDialog(null, // user decides how long array is
"How many numbers are there?"));
double [] numbersArray = new double[howMany]; // creating the array
makeArray(numbersArray, howMany); // calling the array

[code]...

View Replies View Related

Void Method Recursion Error

Jan 11, 2015

i'm trying to exit the recursion of a void method but some funny things are happening before and after the return statement.

public static void main(String args[])
{
//code
rec(x,y);
System.out.println("HEllooooooo");}
public static void rec(int x,int y)
{
try
{
System.out.println(x+" "+y+" "+check);
if(x==fx && y==fy)

[code]....

View Replies View Related

Type Mismatch - Cannot Convert From Void To JTextField

Jun 9, 2014

My GUI class:

import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JPasswordField;

[Code] ....

Eclipse error message:

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Type mismatch: cannot convert from void to JTextField
 
at GUI.<init>(GUI.java:58)
at Apples.main(Apples.java:7)

I copied this from a thenewboston tutorial and he uses an old version of java but im pretty sure ive copied character for character!

View Replies View Related

Private Static Void Print Section

Dec 10, 2014

I have problems with private static void print section, something is missing? And in case 4, I want it to stop the program(end the loop) but it keeps going.

import java.util.Scanner;
import java.util.ArrayList;
public class Dogregister16 {
public static ArrayList<Dog> dogregister = new ArrayList();
private static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) {
initiate();

[Code] ....

case 4:
System.exit(0);
System.out.println("Exit program");
}
}
}
}

View Replies View Related

Rectangle With Numbers - Result Type Is Void

Jun 12, 2014

I want to write a method which would give us a rectangle with numbers. The input is an integer which represents the length of a line in rectangle and the result type is void. The example for n=4 is:

3210
0321
1032
2103

View Replies View Related

How To Write Test Cases For Void Methods

Mar 19, 2014

I'm trying to write test cases for void methods. Here is my sample code

public void objIntoFile() throws Exception {
addAdminObjects();
file = new File("D:ExtractingDBexport.txt");
writer = new BufferedWriter(new FileWriter(file));

[Code] .....

View Replies View Related

Mockito / Powermock Private Void Method

Jul 29, 2014

I need to mock a private void method which takes no arguments using mockito and powermock.

The method belongs to a instance which is a spy.

I am working with an old project converting the unit tests from one testing framework to another.

View Replies View Related







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