Pythagorean Theorem Program - Code Isn't Working When Written In OOP Style

Feb 11, 2014

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);

[code]....

View Replies


ADVERTISEMENT

Pythagorean Theorem Java Applet - How To Get Hypotenuse Of Sides A And B

Apr 3, 2014

I am not sure whats wrong with my code. I need to get the hypotenuse of sides a and b and I am having problems in the code.

// The "Components" class.
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
 public class ButtonDemo extends JFrame implements ActionListener {
// Declare objects
JLabel label;

[Code] ....

View Replies View Related

Program That Counts How Many Times A Letter Is Written

Sep 29, 2014

I can't get my code to print it like it's on the picture.

import javax.swing.JOptionPane;
import java.util.*;
public class BokstavTeller
{
public static void main( String[] args ) {
String input = JOptionPane.showInputDialog(

[Code] .....

View Replies View Related

Everything In ArrayList Written Twice

Mar 1, 2014

I am trying to read a file into an array list, change some things about it, and then write it to another file. Files are getting written with what was read repeating a second time. I've tested the read method by having it print everything which goes into the ArrayList object. I then did the same with the write method which seems to write everything twice, as if everything is in the ArrayList twice, but I'm not sure how this is possible, since the read clearly did everything only once. Perhaps the ArrayList isn't the best collection to use for this purpose.

The read method:

Java Code:

private ArrayList<String> read(File in) {
String line;
ArrayList<String> data = new ArrayList<String>();
try {
BufferedReader r = new BufferedReader(new FileReader(in));

[code]....

View Replies View Related

Pythagorean Triples Using A For Loop

Sep 10, 2014

I have generated many errors in my code for this program but I particularly want to focused on the first error that comes up. Here is the outline of my code:

//a java application that prints the table of pythagorean triples that go up to 500 using a for loop
public class luby{
 //main method executes java application
 public static void main(String args[]){
 //declare variables
 
for(int side_1=1; int side_1 <=500; side_1++){
for(int side_2=1; int side_2 <=500; side_2++){
 
[Code] ....

Here is the output of my code when I attempt to compile it:

C:UsersCourtneyDesktopchapter five exercises for javaluby.java:12: error: '.class' expected
for(int side_1=1; int side_1 <=500; side_1++){
^
C:UsersCourtneyDesktopchapter five exercises for javaluby.java:12: error: illegal start of expression
for(int side_1=1; int side_1 <=500; side_1++){

[Code] ....

12 errors

Tool completed with exit code 1

The first error I want to focus on is the class error; I will focus on the remaining errors once this error is resolved

View Replies View Related

Code For OCR Scanner Not Working As Applet

May 18, 2014

I need to make a scanner (that has a built in ocr) output some content to a browser. I haven't gotten to the browser yet, but the code works when I run it as an application. With the scanner, I received Java code that makes the scanner take a picture, then read from it and output it back to the console. I added a few lines to make it an applet:

Java Code:

import gx.*;
import pr.*;
import java.applet.*;
public class DocScan extends Applet {
/**
*
*/
private static final long serialVersionUID = 1L;

[Code] .....

I am using Eclipse as an IDE. Right now my goal is to simply make the scanner "flash". I know that the output is to the console and I will not see anything from it in an applet, but it should still flash.

When I run this code as an application, it works. The scanner takes a picture and then it outputs what it has read to the console.

When I run this code as an Applet, the applet starts and does nothing. It just stays there with no errors of any kind (at least that's what Eclipse is showing me).

I read that I should allow the applet access, so I edited:

Java Code:

c:program filesjavajre8libsecurityjava.policy mh_sh_highlight_all('java');

And added this at the end:

Java Code:

grant {
permission java.security.AllPermission;
}; mh_sh_highlight_all('java');

Which should allow applets full access. However, there is no change - the applet still launches and does nothing. Why is the code not working when I run it as an applet?

View Replies View Related

Servlets :: Email Sending Code Is Not Working?

Mar 20, 2014

package signup;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates

[Code].....

View Replies View Related

Moving Object In A Frame - Code Not Working

May 7, 2014

I have made a code that should allowed me to move an object in a frame, using the arrow keys. But I don't know why, is not working.

package joc;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.ImageIcon;

[Code] ....

View Replies View Related

Cipher Code - Encryption Test Cases Not Working

Oct 1, 2014

I have following Cipher Code and test for it. But I am getting following exception.

Java Code:

java.lang.IllegalStateException: Cipher not initialized
at javax.crypto.Cipher.checkCipherState(Cipher.java:1672)
at javax.crypto.Cipher.doFinal(Cipher.java:2079)
at com.anjib.util.CipherUtil.encrypt(CipherUtil.java:67)
at com.anjib.util.CipherTest.testEncryptDecrypt(CipherTest.java:23) mh_sh_highlight_all('java'); Java Code: public class CipherUtil {
private static Logger log = Logger.getLogger(CipherUtil.class);

[Code] ....

View Replies View Related

Calculate Area Of Triangle By Herons Formula And Tell If It Is Equilateral Or Pythagorean

Feb 24, 2014

Basically its a program where a user is prompted to enter the length of all three sides of a triangle and the program calculates the area by herons formula and can tell if the triangle is equilateral or Pythagorean. I am having trouble entering a formula to where all three enter sides cant possibly be a triangle. Here is my Program. Where the '?' is stated.

import java.util.Scanner;
public class Triangle {
public static void main(String[] args){
double a;
double b;
double c;
double s;
double x;
double area;

[Code] ....

View Replies View Related

I/O / Streams :: Reading From And Writing To A File Then Read Again Including Data Written

Oct 11, 2014

I'm having a bit of trouble with using the Scanner and the Printwriter. I start with a file like this (1 = amount of Houses in the file)

1
FOR SALE:
Emmalaan 23
3051JC Rotterdam
7 rooms
buyprice 300000
energylevel C

The user gets (let's say for simplicity) 3 options:

1. Add a House to the file,
2. Get all Houses which fullfil requirements (price, FOR SALE / SOLD etc.) and
3. Close the application.

This is how I start:

Scanner sc = new Scanner (System.in);
while (!endLoop) {
System.out.println("Make a choice);
System.out.println("1) Add House");
System.out.println("2) Show Houses");
System.out.println("3) Exit");
int choice = sc.nextInt();

Then I have a switch for all of the three cases. I keep the scanner open, so Java can get the user input (house = for sale or sold, price = ... etc). If the user chose option 1, and all information needed is inputted and scanned, the House will be written to the file (which looks like what I typed above).

For this, I use try (PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("Makelaar.txt", false)))). This works perfectly (at least so it seems.)

If the user chose option 1, and all requirements are inputted and scanned, the Houses will be read (scanner) from the file and outputted. For this I use the same Scanner sc. This also works perfectly (so it seems atleast).

My problem is as follows: If a House has been added, I can only read the House(s) which were already in the file. Let's say I have added 2 houses, and there were from the start 3 houses. If option 2 is chosen, the first 3 houses will be scanned perfectly. An exception will be caught for the remaining 2 (just added) Houses. How can I solve this? I tried to close the Scanner, and reopening it, but apparently Java doesn't agree with this

View Replies View Related

Allman Style For Eclipse

Jan 25, 2012

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.

View Replies View Related

Style Preferences For Conditional Returns?

Oct 21, 2014

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).

View Replies View Related

Font Style Constants To String Is It Possible?

Jan 20, 2014

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?

View Replies View Related

JavaFX 2.0 :: Remove Style From Component?

Oct 28, 2014

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.

View Replies View Related

How To Separate Single Calculator Class (OOP Style)

Sep 5, 2014

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..

here is my code:

import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.Button;
import java.awt.Menu;

[Code] ....

View Replies View Related

JavaFX 2.0 :: Style Column Like Header In TableView

Jul 5, 2014

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.

View Replies View Related

JavaFX 2.0 :: Keep Last Button Pressed With Custom Style

Dec 27, 2014

I have a VBox with 20 buttons and I have this style applied (menu.css) (only for VBox wrapper):
 
.button:focused {
    -fx-background-color: #0768A9;
    -fx-text-fill: #FFFFFF;
}

But when I pressed other button outside the VBox the style disappears, because the other button get the focus.

How I can do to keep the last button pressed with a custom style? (Only for buttons containing the VBox).

View Replies View Related

FileHandling Program Is Not Working?

Apr 28, 2015

this is my progarm

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;
public class FileHandlingDemo {

[code].....

As you can see my Intention is to save randoly generate Integer in the output file And this is not working but if I use Something like this

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileHandlingDemo {

[code].....

it's working fine reading that from Input.txt and writing that data in output.txt file. but not in first case Both file are in working directory already created.Where is wrong?

View Replies View Related

Inventory Program - CompareTo Not Working

Jul 16, 2014

My instructor gave me this code to use as my Array sort. I can not get this to work. I do not get any compile errors, the code just does not do anything.

Java Code:

package inventoryprogram4;
import java.util.Scanner;
public class Inventoryprogram4
{
static GUI mainGUI = new GUI();
static String outText = "";

[Code] .....

View Replies View Related

Working On A Contact List Program With A GUI

Nov 27, 2014

I am working on a contact list program with a GUI for a school project.I'm running into all kinds of issues when trying to update the JList in my GUI which displays the contacts.I think my issues are being caused by a lack of synchronization between the contact list that stores the data, and the contact list that is being displayed.I've tried all kinds of getters and setters, passing array indexes, passing array member objects etc.

General rundown of how I want the GUI/Code to work: When I select an object in the JLIst, the fields on the right should display that objects information.When the "Add Contact" button is created, a new blank contact should be created and added to the Contact List, and then displayed in the JList.When a field on the right side of the GUI is updated, the object in the contact list is updated simultaneously (using document listener), and therefore the JList would also be updated.

View Replies View Related

Program For Calculating Circumference And Area Not Working

Jul 15, 2014

Here is the program I wrote for calculating the circumference of a circle.

//circumference.java
//program that calculates the area and circumference of a circle
import java.util.Scanner; //program uses class scanner
public class circumference{
//main method begins execution of Java application
public static void main(String args[]){

[Code] .....

Even though my code compiles and runs without any errors, the program doesn't calculate the circumference.

View Replies View Related

Program Involving For Loop And If Statement Not Working

Oct 23, 2014

Alright so I'm trying to write a code that implements a for loop and if statements that displays any number from 100-200 if the number is divisible by either 5 or 6 in rows of ten numbers each row. If it is not divisible by that number then it should go back to the beginning of the loop until it reaches 200. My main problem is that it doesn't display anything. I don't get any errors or anything but every time I run the program it just displays nothing. Sample output is at the bottom of the code.
 
public class Exercise5_11 {
public static void main(String[] args) {
int count = 0;
int i = 100; 
//for (the numbers from 100 to 200)
for (i = 100; i>100 && i<200; i++){
 
[Code] ....

/*Output
100 102 105 108 110 114 115 125 126 130
132 135 138 140 144 145 155 156 160 162
165 168 170 174 175 185 186 190 192 195
198 200
*/

It is still wip of course so I was trying to just get it to display int i but it doesn't do anything and I'm not really sure why.

View Replies View Related

Java UDP Client Server Program Not Working

Apr 18, 2014

I am currently writing two java classes (client and server). The client takes an input number form keyboard and sends it to the server. The server then multiplies this number by two and sends it back to the client. The numbers should also be printed to screen along the way, for example if I input the number 3 I should get

"From Client: 3" "From Server: 6"

They should continuously do this unless a negative number is received by the client, say for example the number -3 is sent to the server and it returns -6.

The code I have for the two classes so far is:

import java.io.*;
import java.net.*;
import java.nio.ByteBuffer;
import java.util.Scanner;
class Client {
public static void main(String args[]) throws Exception {
DatagramSocket clientSocket = new DatagramSocket();

[Code] .....

Currently, when I run the program all I get is an output of the number first entered. I am aware it requires a loop but I don't know where and what the condition should be.

Also if I wanted to adapt this so that it would take the integer from client and subtract two at the server and return to client who sends back to server to keep subtracting two unless it reaches a negative number at which point the client will terminate the program - how might I do this.

I do realise there needs to be a while loop in the above code, but I wanted to test it sent the number from client to server and its not doing it. All I get is a print screen of 'enter number' and then the number I enter.

View Replies View Related

Working On A Program That Creates Array From A File

Feb 19, 2015

Working on a program that creates an array from a file, organizes it in various ways, and then prints the results. Right now I'm specifically having trouble with the printAry() method - when I try to run it, I receive a NullPointerException for the active line in printAry() (EDIT: line 102). Not sure which sections are important to this, so I will include the client class and most of the service. I realize that the amount of documentation I included is not necessary, but it is required for the class.

class Lab3StudentAryService
{
private int count;
private Lab3StudentData [] studentAry = new Lab3StudentData[50];

/*
@param: none
@return: none
reads file and converts to array.
*/
public void fillAry() //constructor
{
Scanner scan = new Scanner(System.in);

[code]....

View Replies View Related

Program Is Working But String Index Out Of Range Error

Sep 5, 2014

My Computer Programming teacher has given the class a problem that requires the use of var.charAt(), var.length() and while/for. Basically, the problem is that we have to create program that'll show a mirrored or reverse version of the entered word. Like for example, if your input is the word "Hello" (the quotation marks aren't included), the output will be "olleH".

Here's the code:

import java.io.*;
public class StringMirror
{public static void main (String [] args) throws IOException
{BufferedReader scan = new BufferedReader (new InputStreamReader(System.in));
String enteredWord = "";
int lengthOfTheWord = 0;
int lengthOfTheWordMinusOneVersion = 0;

[code]....

It is working, but the problem is that after the output, there's this error that says String index out of range: -1.

the program is working but I kind of wanted it to have no errors in the end.

View Replies View Related







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