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);
}
}
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?
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');
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.
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");
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; }
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"...
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?
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"
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. }
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
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
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?
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
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"); } } } }
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: