How To Apply Casting Correctly For HeightProgram

Mar 17, 2014

how to calculate the child's height in float value fixing value where if you choose male the accurate value, but if you choose female the value will be accurate too.

int heightMother, heightFather;
int heightMaleChild, heightFemaleChild;
String gender;

[code]....

View Replies


ADVERTISEMENT

How To Apply Format Pattern

Jan 12, 2015

I am able to get Cpu speed using my GetProcessorSpeed method and It returns this output 1796. How can apply this pattern "#.##". I am trying something like this.

Format formatter=new DecimalFormat("#.##");
formatter.format(MainClass.GetProcessorSpeed());
label2.setText(formatter.toString());

View Replies View Related

How To Apply Electronic Signature To Predefined XML

Oct 4, 2014

I must apply a electronic signature to a predefined XML.
 
File looks like this:

<xml....
<invoice serialCode="NT" serialNumber="123" issueDate="2014-10-01" startDate="2014-09-01" endDate="2014-09-30" orgUnitCode="CAS-NT" providerCode="15107618" providerCategory="PARA" contractNumber="8207" contractType="PAR" contractDate="2014-06-27" totalAmmount="34.81"
 
And at the end there is a sinature that looks like this:
‚ R0‚ N0‚ 6          $P Gæ&"ùO]ó]0
  *†H†÷
0h1%0#  U    DigiSign Qualified Public CA1 0   U    DigiSign Public CA1 0   U
  DigiSign S.A1 0   U    RO0 
140722065817Z

[Code] ....

How can I do this in java?

View Replies View Related

Why Overridden Doesn't Apply To Instance Variables

Mar 15, 2014

why overridden doesn't apply to variables. However, instance variables are stored inside the object.I ran below program and expected to print "two" but it gets printed "one".

class SupCont {
String s = "one";
}
class Cont extends SupCont {
public static void main(String a[]) {
String s = "two";
SupCont c = new Cont();
System.out.println(c.s);
}
}

View Replies View Related

Timestamps And Time Zones (DB2 Oriented But May Apply To Other Databases As Well)

Dec 15, 2014

If you store a Timestamp in a database (in my case, I'm looking at DB2), doesn't something on the back end convert it to UTC?  And then when you read it back out, it gets converted to the local time zone?  How does this happen?
 
Lets say you are using a computer in one time zone, but the database is running on a server in another time zone.  What conversions take place upon storage and then retrieval?
 
And here is where it gets a little more Java specific. Using a PreparedStatement or a ResultSet, you can change the time zone when setting and retrieving a Timestamp.
 
Example:
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
ps.setTimestamp(2, timestamp, calendar);
 
And you can read it out from a ResultSet in a similar fashion.
 
However, isn't using such methods technically wrong?  If the database is trying to store your instant in time accurately, doesn't specifying a Calendar other than the one in your local time zone cause the database to actually store an incorrect instant in time?
 
So I need to read Timestamps from a database, and I've been told they are stored as GMT.  This has caused me great confusion.  I think what I need to do is just read it out from the result set using a Calendar set for GMT timezone. 

View Replies View Related

Casting For CompareTo

Nov 11, 2014

I am trying to compare some items from a generic arraylist with each other, but I keep getting an error stating that I need to cast the values in line 38. However, when I heed the warning and change it to what it wants, I get a warning stating "type safety: Unchecked cast from K to Comparable<K>". Should I ignore this warning or is there a better way to compare the two items? Also, is there another way for me to use compareTo w/o making my class extending/implementing comparable or is that the only way?Here is what I have:

class WordInfo<K, V extends Comparable <K>> {
private FileReader fr;
private String word;
private ArrayList<K> list;
private BufferedReader br;
private int current = 0;

[code]....

View Replies View Related

Casting Exception In Generics

Jan 29, 2014

I have studied that Generics are used to shift the Class Cast Exception into Compile time errors , So that we get errors at compile time error and we do correct them before executing ,but Here is a program in which i am getting Class Cast Exception

class Animal
{
}
class Dog extends Animal
{
}
class Cat extends Animal

[code]..

Getting Exception at line no 29 which i know why it occurs but just wanna ask that isn't it should be caught at compile time According to Generics ?

View Replies View Related

Implicit And Explicit Casting

Aug 21, 2014

The following code snippet will print 'true'.

short s = Short.MAX_VALUE;
char c = s;
System.out.println( c == Short.MAX_VALUE);

Correct Option is : B

A. True

B. False

Explanation:

This will not compile because a short VARIABLE can NEVER be assigned to a char without explicit casting. A short CONSTANT can be assigned to a char only if the value fits into a char.

short s = 1; byte b = s; => this will also not compile because although value is small enough to be held by a byte but the Right Hand Side i.e. s is a variable and not a constant. final short s = 1; byte b = s; => This is fine because s is a constant and the value fits into a byte. final short s = 200; byte b = s; => This is invalid because although s is a constant but the value does not fit into a byte. Implicit narrowing occurs only for byte, char, short, and int. Remember that it does not

occur for long, float, or double. So, this will not compile: int i = 129L;The below code compiles fine and contradicts what is said in bold. So what does the bold statement mean then?

Java Code: class BreakTest{
public static void main(String args[])
{
float f=1.0f;
double d=f;
}
} mh_sh_highlight_all('java');

View Replies View Related

Typing By Casting To Interface

Jun 16, 2014

I'm looking for a heuristic explanation of how to think of an "interface" as a type. I'm used to think of the 'type' of a class coming form its very definition but I often see casting to an interface which I still feel very uncomfortable about.Other than an interface, are there other unusual ways a 'type' may be referred to?

A second basic question: When you user 'super.f()', will Java go up the calling chain until it finds method 'f' (and report an err if none is found) or does it expect to find 'f' immediately at its very first parent?

View Replies View Related

String Input / Output And Casting

Oct 2, 2014

I have to create a program that uses the Scanner in String input and Output that will enable the user to convert

double -> int
int -> byte
int -> short
int -> hex-string

The output should look something like this:

Syntax: convert <value> <type>
convert 15.5 int~> 16
convert 128 byte~> -128
<type>: int, byte, short, hex, decimal
Syntax: max <type>
max byte~> 127
<type>: int, byte, short

[Code] ....

View Replies View Related

If Statement - Char To String Casting?

Aug 3, 2014

import java.io.IOException;
import java.util.*;
public class Guesser {
public static void main(String[] args) throws IOException {
 char[] alphabet = "abcdefghijklmnopqrstuvwxyz1234567890 .,:;'-".toCharArray();

[Code] .....

I'm writing a program which will take a three letter word (for now) and then try to guess the word over and over again until it finds it, then print the word and the amount of tries it took to find it.

The problem: at the moment the program will find the word but not break out of the for loop when it does. I think it doesn't like the char to String conversion somewhere along the line.

View Replies View Related

Servlets :: Why Session Not Type Casting Into String

Oct 17, 2014

why my session not type casting into String? I'm placing that code below where problem arised.

HttpSession hs1=request.getSession(false);
out.println("hi");
String t1=(String)hs1.getAttribute("name");
out.println("hi "+t1);

in my above code first "hi" is printed successfully but next statement arises type cast exception.

View Replies View Related

Casting To Parameterized Types And Unchecked Warnings

Jul 15, 2014

There is a sentence in JLS 7 which I can't figure it out. It says :

A cast from a type S to a parameterized type T is unchecked unless at least one of the following conditions holds:
-S <: T
-All of the type arguments (§4.5.1) of T are unbounded wildcards
-T <: S and S has no subtype X other than T where the type arguments of X are not contained in the type arguments of T.

Condition one and two I got it. But the number three is really bugging me. I write some code in order to try to understand it.

class G<X>{}
class D<T,U> extends G<T>{}
G<String> g = new G<>();
D<String, Integer> dd = (D<String, Integer>) g;

In Eclipse I got no warning but it shouldn't give one ?

Because g has others subtypes than D<String, Integer> (e.g. D<String, List> , D<String, G>)

Am I missing something about the contained type arguments ?

View Replies View Related

Casting And Interfaces - Comparing Object Of Unknown Types

Oct 15, 2014

import java.util.*;
public class CommonElements
{
private int comparisons; // number of comparisons made
private Comparable[] arrayToSearch; // current array being traversed
private Comparable[] commonElements = new Comparable[10];
private int arrayPosition = 0; //keeps track of what index to add an element to common at

[Code] ...

I have trying to get this down to the bar minimum. I am trying to cast the desired object array to a array of comparable. This is all required by the assignment.

I am getting a runtime error that I can not perform the desired cast. What do I need to provide the compiler in order to allow for this casting. I can not change the signature of the method however nothing about the class has been specified do I need to implement comparable? Also I don not now what the client is passing so how would I write a generic compareTo method to compare object of unknown types.

View Replies View Related

Type Casting - Getting Number With Two Decimal Places As Output?

Jun 7, 2013

"Type Casting" .... So the question is "How am I getting a number with two decimal places as output?"

import java.util.Scanner;
public class SalesTax
{
public static void main(String[] args) {
double purchase, tax;
Scanner input = new Scanner(System.in);
System.out.print("Enter purchase amount: ");
purchase = input.nextDouble();
tax = purchase * 0.06;
System.out.println("Sales Tax: $" + (int)(tax * 100) / 100.0); +//this will give two decimal places??+
}
}

View Replies View Related

Why If Statement Isn't Working Correctly

Apr 23, 2015

I am building a program that when you enter 1. it allows you to setup an item. However running my code my second if statement runs through.

import java.util.Scanner;
public class InventorySystem {
public static void main(String [] args) {
Scanner input = new Scanner(System.in);
int count=0;
int inputEntered=0;
int numberOfItems=0;
double cost=0.00;
String item;
String description;
 
[code]...

View Replies View Related

How To Read A File Correctly

Feb 13, 2014

So i am creating a File object which has a text file passed to it. I then try to do logic with it using a BufferedReader. However, I get a FileNotFoundException on the using my code below. The Error is on the BufferedReader line. I

Java Code: System.out.println("--Reading text file--");
File file = getFile(c,fileName) // Returns a File object.
System.out.println(file); // Shows me the file is looking correct. Displays contents to console.
BufferedReader br = new BufferedReader(new FileReader(file));
System.out.println("BUFFERED");
while((line = br.readLine()) != null) {
try {
// Do Logic
}
catch(Exception ex){
ex.printStackTrace();
}
br.close(); mh_sh_highlight_all('java');

View Replies View Related

If Else / Switch Not Outputting Correctly

Jan 27, 2014

Trying to learn switch statements. Can't figure out what I am doing wrong.

java Code:

import java.util.Scanner;
public class Ok1 {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);

[code]...
When I run, it makes me enter letter = null and it wont output correctly.

View Replies View Related

Write Correctly Border And Gravity

Sep 27, 2014

I want to write simple game. Why if I press button UP and RIGHT it not move diogonally, may be it have special code. And how I need to write correctly border and gravity?

public class Main {
public static void main(String args[]) {
JFrame frame = new JFrame("Stickman");
Ground ground = new Ground();
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setSize(710, 480);

[code]....

View Replies View Related

GCanvas In ACM Graphics Not Working Correctly

Mar 25, 2014

I am trying to implement a game using the ACM graphics. For the game, I am trying to make the main frame a Grid of Cells.I made a Grid class which extends GCanvas:

import acm.graphics.GCanvas;
public class Grid extends GCanvas{
private final static int WIDTH = 300;
private final static int HEIGHT = 300;
private final int DIMENSION = 5;
Cell[][] grid;

[code]....

View Replies View Related

Game Over Method Not Updating Correctly

Sep 4, 2014

If I run this code. I can manipulate it so that X wins. When the third X button is pressed, there is no display saying that X has won and that the game is over. I have read over this code for days and still have not figured it out.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 public class TicTacToe extends JFrame implements ActionListener, WindowListener{
 public JFrame window = new JFrame("window");

[Code] ....

View Replies View Related

How To Drag JLabel Correctly Across JFrame

May 26, 2014

I am currently trying to make a method that will enable a jLabel to be dragged across the screen. I want to be able to drag the object pretty much anywhere on the screen(I hope to eventually make a destination for it where it can be dropped but I will get to that later). I have looked on various places on the internet on how to achieve this and the most simple solution does something like this:

Java Code:

jLabel6.setText("jLabel6");
jLabel6.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
public void mouseDragged(java.awt.event.MouseEvent evt) {
jLabel6MouseMoved(evt);
}
});
private void jLabel6MouseDragged(java.awt.event.MouseEvent evt) {
x = evt.getX();

[Code]...

This actually works somewhat but it is very faulty. For example the jLabel keeps appearing and reappearing as I drag it. Furthermore when I drag it I can see a duplicate Jlabel being dragged as well in the top left hand screen. So how might I fix this?

View Replies View Related

Cannot Get Numerical Date To Display Correctly

Nov 29, 2014

I am trying to write a date program. I have it written and running correctly, except one issue. I cannot get the numerical date to display correctly. I know it is an issue with printf. I don't know anything about printf. Here is my method to return the day

public int getDay() {
return day;
}

When it returns, it is literally returning just the numeric date. I need it to show up as 02 or 03 instead of 2 or 3, respectively.

View Replies View Related

Date Order Not Printing Correctly

Sep 8, 2014

the dates are printing out of order. It should appear MM/DD/YYYY however with my code its printing DD/YYYY/MM.Here is my code.

public class DateTest {
public static void main(String args[]){
Date date1 = new Date( 7, 4, 2004 );
System.out.print( "The initial date is: " );
date1.displayDate();

[code]....

View Replies View Related

AverageDriver Won't Pull Data Correctly

Dec 12, 2014

I'm unable to get the output for "AverageDriver" to print correctly(5 scores in descending order and average). The output displays the scores the same way they are entered and the average is always zero. The output for the Average program works fine and with the correct output.

import java.util.Scanner;
//This class keeps 5 scores entered by the user
public class Average
{
private int[] data;
private double mean;
public Average()

[Code] ....

View Replies View Related

Cannot Get Data File To Print Out Correctly

May 7, 2014

I cannot get the data file to print out correctly when I go to the display case. It only prints one name three times.
 
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import java.util.Scanner;
import java.io.*;
import java.text.*;
import java.math.*;
import java.util.StringTokenizer;
 
[Code] ....

Output when I choose Case 1:

Minia
Minia Jane
Minia Jane

View Replies View Related







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