I have a routine that returns a boolean. If any of a series of tests fails, the routine returns false, otherwise it returns true. For example, the routine might test for whether or not an integer is both odd and greater than 99, thus:
public boolean oddAndOld(int x)
{
if (x % 2 == 0)
return false;
if (x < 100)
return false;
return true;
}
I like the above because it suggests that "true" is the condition that applies if the incoming parameter meets all the required criteria. But I've sometimes seen this style used:
public boolean oddAndOld(int x)
{
if (x % 2 == 0)
return false;
if (x < 100)
return false;
else
return true;
}
I like this less because, among other things, if that last criterion is removed, the "else/return true" must be moved up into the immediately preceding test (or else leave some funny whitespace, depending on how you go about removing the departing "if" statement), but it does avoid suggesting that "return true" is hard-coded (that is, it reinforces that "true" is a conditional return value, not inevitable).
I'm learning Java and came across saving user preferences. As I was learning I also learned about object serialization and File input/output. I've heard that the windows registry can be a problem because it can quickly become a dumping ground for uninstalled programs. So my question is why should I use the Preferences API? and is it really good practice to program to the registry for saving information? I'd also like to be in good standing towards the programs I write so if it is common practice to write to the Registry how exactly would I manage the information that I dump into the Registry?
public class dowhile2 { public static void main(String[] args) { int i = 1; while(i <= 100){ System.out.println(i); if(i == 50){ break; } i++; } } }
I just want to know how to count this lines of codes. Because if I place for example i++ further up the code or the system.out.println a bit lower the result will be 1-49 or 2-50 and not the goal that is 1-50.
I want to know how to think and count. Why do i get 2345678.........50 and not 12345678........50 or 12345678.....49, thats the question.
It shows some Error Message as "DXE compiler error. No table 'GTGT' found in DXE. Source name: DxeAPI. XML: None". It it possible to add some condition for Grand Total? If so, how I alter my XML??
I am new to java and this seems like it has to be really simple, but so far I am unsuccessful. I want to create a boolean value if a certain condition is met. I need my application to compare who is logged in to my web app with the value for who created a particular report.
I retrieve the creator of a report in report.getCreatedBy() and I can display the current user with user.getName(). I need my application to determine if the person logged in is the same as the createdBy value and then set isAuthor() = true
Then I want to user the isAuthor true or false to display certain information in my jsp. That part I can handle, just not sure what I am missing / have incorrect from above.
I just started Java programming bout 3wks ago and using jGrasp since then. But ive started using eclipse and just wondering if it can be set up to use allman style? I dont like having to use tab and backspace to get my brackets in line and neat. I decided to ask before i went snooping around the settings and mess something up since im not to familiar with it right now.
The Font style constants are of int: int[] fontStyleList = new int[] {Font.PLAIN, Font.BOLD, Font.ITALIC};But what do I do when I want to add the names such as PLAIN etc. as the name of a menu item?
The following is not going to work, right!String[] parts = (fontStyleList[i].toString()).split("."); or String[] parts = ("" + fontStyleList[i]).split(".");rbStyle = new JRadioButtonMenuItem(parts[1]);
Is there another way to do this or should I just do another array with the names as a String?
I am using the Javafx 8 Date Picker. Its works fine. I have defined a stylesheet for my application and in that style sheet i have a style for Button. I apply this style to the every scene. Now my question is: When I use the datepicker and I click on the calendar icon, the datepicker popup appears and the left and right arrows adjacent to month and year fields in the seems to pick the style of the button class from style sheet defined in my application., Is there a way to avert this and tell datepicker to use default styles and not the styles defined my application.
I tried removing the style at runtime from the scene, but of no use.
I've been into java for about 4 months now, No complaints so far, it's pretty awesome! So I understand Polymophism pretty good, I did some Animal classes and it worked out neatly.
However. I just can't seem to find my way in OOP when trying to implement in on a Calculator Class I wrote. As you mind know, a calculator can be separated into many classes. But I actually just want it to be separated into some OO like: | Calculator | CalcGUI | CalculatorFunctions | CalcRun.
I tried separating the single Calculator class, however, my calculator Buttons didn't work after that, anymore..
I have a column of my TableView that just shows row numbers, and I would like to style the cells in that column so that they appears just like a column header (so the same styling as the headers use). I presume there is some simple way to do this by tapping into the right style class, but I am not sure which one.
I'm attempting to make a simple Pythagorean Theorem program (A squared + B squared = C squared) but am running into problems when I write it as object oriented. The darn thing works when written as a simple process, but that isn't Java now is it? Here's the simple:
public class PythagoreanTheorem extends ConsoleProgram { public void run() { println ("Finding C from A and B."); double a1 = readDouble("Input A: "); double b1 = readDouble("Input B: "); double aSq = (a1*a1); double bSq = (b1*b1); double cSq = (aSq + bSq); double c = Math.sqrt(cSq); println ("C = " + c);
I've been trying to make a class that has a toString method that displays the board at the same time displays the value of each index of a 2d character array inside of the code. My professor has made the client print the method. I assumed since he was printing it on the client we had to return a string that gives us this.The number 2 is part of the 2d character array while the rest of the index values are just space characters.
I had trouble figuring out how to return this so I started testing it out in another file.
public static void main(String[] args) { // TODO Auto-generated method stub char[][] myHidingPlaces; myHidingPlaces = new char[5][3]; myHidingPlaces[0][0] = 'p'; char play = '1'; for (int i = myHidingPlaces.length-1; i >= 0 ; i--) {
Basically, it's to write a method that takes in, and then returns another array, whose first element is the average of the first two numbers, last element is the average of the last two, and then everything else is just the average of that index, the one before, and the one after. {1, 2, 4, 8} would return {1.5, 2.33333, 4.66666, 6}.I'm currently getting everything fine, except am not getting the last number (i.e. '6').
public class Arrays { public static void main(String [] args){ double [] a1 = {1, 2, 4, 8}; for (int i = 0; i < a1.length; i++) System.out.println(a1[i]);
Before I created the data file are the points of x and y I created a model class that represents the graph. I have a problem with making a function that returns the panel of the graph.
Attached File(s)
data.cvs.txt (726bytes) Number of downloads: 12 chart.txt (3.72K) Number of downloads: 10
I need to create a method that returns a new array containing the componentwise sum of its arguments(if length is the same). For instance, if the input arrays are {0,1, 2} and {2, 2, 3} then the output is {0+2, 1+2, 2+3}, i.e. {2,3,5}.If the input arrays have different numbers of elements, the method should return null.
I came with something like this, however i dont know how to make a copy of an array from two arrays. My code obviously wont compile. package whatever;
import java.util.Arrays; public class hhhh { public static void main(String[] args) { double [] a = {1,2,3}; double [] b = {2,3,4};
I'm currently having trouble with a Breakout clone that I'm working on when I want to change the scenes of the game. When I start out with the main menu and I hit play, the main menu calls for the StateManager to remove the MainMenu and then to add the GameState to start playing through the levels. But, whenever this is done it just gives a NullPointerException and freezes the game. I know the code for initially starting a State is fine because that is the same way the MainMenu is loaded but there seems to be an issue with changing from one to another...
This is the StateManager class that adds and removes the States/Canvases to and from the JFrame/Main class:
public class StateManager { private ArrayList<State> states; private int currentState; private JFrame gameFrame; public StateManager(JFrame gameFrame) { this.gameFrame = gameFrame; states = new ArrayList<State>();
[Code] ....
I've tried messing around with the order of which things are added/loading from the StateManager but am just failing to see what I did wrong.
Working on chapter 10 of my book which covers exceptions and file I/O. The only part that does not work is line 40 where it is supposed to print getMessage(). It print's null instead of the more verbose error message.
import java.io.*; import java.util.*; import java.text.DecimalFormat; public class SalesReport2 { public static void main(String[] args) { String filename = "SalesData.txt"; int months = 0;
[Code] ....
temp.txt
123.33 asdf 549.85 dsaf 8456.23 21588.22 652.00
My output when using my test file.
ERROR: SalesData.txt does not exist. Enter another filename: temp.txt Nonnumeric data found in file: null The invalid record will be skipped. Nonnumeric data found in file: null The invalid record will be skipped. Number of months: 5 Total Sales: 31,369.63 Average Sales: 6,273.93
I then removed the try catch so that I could get a stacktrace
import java.io.*; import java.util.*; import java.text.DecimalFormat; public class SalesReport2a { public static void main(String[] args)
[Code] ...
This was the output.
ERROR: SalesData.txt does not exist. Enter another filename: temp.txt Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at java.util.Scanner.nextDouble(Unknown Source) at SalesReport2a.main(SalesReport2a.java:32)
I also pulled the copy of the code from the CD that came with the book and it's output is the same as my code.
The image in the book shows that the error should be more informative though.
what I get
Nonnumeric data found in file: null
vs
what I should get
Nonnumeric data found in file: For input string: "asdf"
I'm currently having trouble with a Breakout clone that I'm working on when I want to change the scenes of the game. When I start out with the main menu and I hit play, the main menu calls for the StateManager to remove the MainMenu and then to add the GameState to start playing through the levels. But, whenever this is done it just gives a NullPointerException and freezes the game. I know the code for initially starting a State is fine because that is the same way the MainMenu is loaded but there seems to be an issue with changing from one to another...
This is the StateManager class that adds and removes the States/Canvases to and from the JFrame/Main class: package com.pathtocreating.Breakout_V2.state;
import java.awt.BorderLayout; import java.util.ArrayList; import javax.swing.JFrame; public class StateManager { private ArrayList<State> states;
[Code] ....
The code that calls the change of state in MainMenuState is called in the update method:
Here is the full stack trace when clicking the area to change states:
Exception in thread "Thread-2" java.lang.NullPointerException at sun.java2d.SunGraphics2D.validateColor(Unknown Source) at sun.java2d.SunGraphics2D.<init>(Unknown Source) at sun.awt.image.SunVolatileImage.createGraphics(Unknown Source)
[Code] ...
It gives that same value both before and after clicking on the region.
Printing out b after the State is changed to GameState gives the following:
What is exact difference between them? Another thing is when I check (a==b) it retuns me false, but when I check a.equals(b) it returns me with true. Why So?
Write a Java method that returns the largest value of an array of doubles passed to the method as an argument.
Back into java wasn't sure how to do it for doubles did one in the main for integers and then added a method changed from int to double and now i'm lost as go why its not working.
package kickstarter9; public class Kickstarter9 { public static void main(String[] args){ double myList; double[] myList = {6.0, 4.1, 2.4, 6.8, 1.9, 9.4, 2.8, 4.6, 9.3}; // find the largest value in the list
I have to create a program that calculates the nth Fibonacci number and returns that to the user. Fibonacci said his number sequence would describe the ideal breeding patterns of immortal rabbits. So, you are going to make this vision a reality.
First, take in a numeric value from the user and calculate that value in the fibonacci series. Next, find an image of a rabbit and display the image on a GUI (put the image as an icon on a label!) the number of times returned by the algorithm (Put all the aforementioned labels on one panel with FlowLayout!).
You need to remove the old images from the Panel. Probably the easiest way to do this is to create a whole new panel and remove the old one (hint: the remove method of JPanel should come in handy)
You could use an array of JLabels You will need to create a new JLabel and add each member of the array to the panel I should be able to scroll to see any images that are off screen
I am having difficultly on to making the array list for JLabel, and getting the Fibonacci sequence to show the pictures of rabbits. Below is my current code.
import java.awt.FlowLayout; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Rabbit extends JFrame