How To Structure Simple Java Programs

May 8, 2015

how to structure simple programs (i.e. one-method algorithms for a simple purpose). There are a few different ways that I have been doing it, but I want to be consistent. Should I put both the one-method algorithm (e.g. a factorial function) and the main method that executes the algorithm into the same class and then export it to an executable .jar file for use? Or should I create one class for the algorithm and another for the main method that executes the algorithm? In addition, is there any reason that I should out these classes in a package before I export it?

As another similar question, if I have constructed two distinct classes with two separate purposes, and they are both used in the construction of a single program, then would it be best to just put the main method in a third, separate class or should I put it in one of the two classes?

View Replies


ADVERTISEMENT

Simple Data Classes - Serialize More Complex Data Structure

Oct 6, 2014

I have written several simple data classes that I serialized manually by converting to text. At this point I need to serialize a more complex data structure. which will include lists of the simpler elements. Can serialize store and reconstitute this type of structure automatically or do I need to do this one manually as well? Consider the pseudocode below for a clarification of my question;

Java Code:

Class Hops{
String Name;
float Alpha Acid;
float Beta Acid;
};

Class Malt{
String Name;
float extract;
};

Class Recipie{
String Name;
CList HopList;
CList MaltList;
};

CList RecipieList; mh_sh_highlight_all('java');

I read the article on Serialization presented at tutorial point, but the example only showed a simple class, not lists of class members. What I want to do is serialize RecipieList, which consists of a CList of Recipies, which in turn consist of CLists of various ingredients.

View Replies View Related

Running Programs Through Java

Mar 30, 2014

I've been playing around with this for about an hour.

Java Code:

Runtime runTime = Runtime.getRuntime();
try {
Process process = runTime.exec("notepad");
} catch (IOException e) {
e.printStackTrace();
} mh_sh_highlight_all('java');
So that works. Notepad will open.

However, I'm trying to get other programs to open. Specifically, this program: C:Octave3.2.4_gcc-4.4.0inoctave-3.2.4.exe...However, using that in place of notepad doesn't work. I'm assuming that there is some sort of system variable that explains why simply typing "notepad" works? As if you type notepad into the run box, notepad will open. Soo does that snippet work by going through some sort of system variables?How would I go about opening other programs, such as the one I referenced above.

View Replies View Related

How To Compile And Run Java Programs Using JDK And JCreator

Jan 26, 2014

How do I set the class path, the path, compile and run java programs using jdk and jcreator?

View Replies View Related

Running Java Programs From Command Prompt

Dec 17, 2011

My PC runs on Windows XP. When I try to run a java program (written using NetBeans) from the command prompt, the program opens in NotePad but does not run.This is what I have been typing at the command prompt:

C:javahellosrchelloHello.java

The above is the correct path to the Java file on my PC.how to run the program from the command prompt or perhaps link me to a tutorial that explains it?

View Replies View Related

Trie Data Structure In Java

Sep 27, 2014

Problem in inserting,removing and searching in trie data structure.

View Replies View Related

Parsing Tree Structure In Java

Jun 26, 2015

I have an excel file with data listed as the following, i m trying to parse down using POI
 
A
     B
          C
               D1
               D2
          F
               G1
               G2
               G3
          M
               S1
     R
          T             
     U
L
  X
     Y
         Z
 
is it possible to generate an output like the following
 
A
A-->B
A-->B-->C
A-->B-->C-->D1
A-->B-->C-->D2
A-->B-->F
A-->B-->F-->G1
A-->B-->F-->G2
A-->B-->F-->G3
A-->B-->M
A-->B-->M-->S1
A-->R
A-->R-->T
A-->U
L
L-->X
L-->X-->Y
L-->X-->Y-->Z
 
I have been trying from quite some time but havent figured out the logic ...

View Replies View Related

Java Calculator Using Stack Data Structure

May 21, 2014

My assignment is to design a simple GUI calculator using the stack data structure to perform additions, subtractions, multiplications and divisions. But i having error while i press the action there.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.util.Stack;
public class JCalculator implements ActionListener {

[Code] ...

View Replies View Related

Build Tree Structure From Array In Java

May 31, 2014

I never used tree, node etc. Consider an array of strings that come from html tags. This needs to be turned into a tree structure. Here, any Hn is the child of the most recent Hn-1

String[] Headers = {"H1", "H1", "H2", "H3", "H3", "H2", "H2", "H3",
"H4", "H2", "H2", "H2", "H1", "H2", "H2", "H3", "H4", "H4", "H2" };

Desired output -

ROOT
H1
H1
...H2
......H3
......H3
...H2
...H2
......H3
.........H4
...H2
...H2
...H2
H1
...H2
...H2
......H3
.........H4
.........H4
...H2

ANSWER -

class Example {
static class Node {
final String name;
final int indent;
Collection<Node> children = new LinkedList<> ();

[Code] .....

View Replies View Related

Java Program To Implement A Single Linked List Structure

Jul 27, 2014

I'm trying to build a program that contains the ability to:

(1) insert new node at head,
(2) print out contents of the list in order,
(3) remove first node from head,
(4) remove last node from tail,
(5) find a target value,
(6) give total number of occurrences of a target value, and
(7) give total number of items in list.

The areas I'm struggling with implementing are: (

- remove from tail - I know how to find the final node but I can't quite figure out how to set it to null since its initial type is an integer.
- find a target value - how to make the parameters quite workout so the user can simply input an integer value.
- The solution is probably really simple but I can't figure out how to print out the results of these methods when I call them.

public class Node
{
private int data;
private Node link;
// Node Constructor 1
public Node()
{
data = 0;
link = null;

[code]....

View Replies View Related

Use IDE When Writing Programs

Mar 27, 2014

I was wondering if you use an IDE when writing programs?Can you use J frame when using command line or is it just used with an IDE and do you go into framing in the book?I have taken programming classes and I am still stuck.

View Replies View Related

Java Tree Structure - Build Tree Based On Traversal Results

Jun 17, 2014

How to do draw the original binary tree based on traversal results?

A binary tree has this pre-order traversal result: A,B,D,H,I,E,F,C,G,K,J (TreeNodes) And the same tree gives the following in-order traversal: B,H,I,D,A,C,F,E,K,G,J. Can you draw the tree structure?

View Replies View Related

What Programs Use Abstract Classes

Sep 15, 2014

What programs use abstract classes?

View Replies View Related

Changing Values In Different Programs?

May 20, 2014

I'd like to make a simple cheat for an old game that's offline... It has no type of anti-cheat and I'd like to make a program that changes how much money you have... I think the first thing I have to do is get the data address or something like that by using cheat engine... How do I do that? After that how do I start using that info in my program and then send back a new value? Are there any classes that I should be importing and what methods do I use to do this?

View Replies View Related

Programs To Display Only Odd Numbers Between 1 And 15

Nov 16, 2014

I have a programs to write which is to display only the odd numbers between 1 and 15 and the code i use it is not display what i was expecting it to but instead it looping 15 times and at the end it says that the odd number is 15 or 1. where or what have i entered wrong. it supposed to display only the odd numbers between 1 and 15here is the code that I write.

Scanner input = new Scanner (System.in);
int i, num=0, count = 0, count_odd = 0, oddnum = 0;
for(i=1; i<=15; i++) {
System.out.println("Please enter number");
num = input.nextInt();
}

[Code]....

View Replies View Related

Resize Window To See Programs

Jan 21, 2014

Whenever I make something in Swing, I always need to resize the window for me to see the graphics on the window. I have been told to use the pack() method on it, which I have, but the problem with it is that it just sets the window to a minimal size, only displaying the x + and -.For one of the "games" I made, it at first didn't work until resize, then I added a repaint() after the constructor and it worked for me, but when my friends download it they cannot see it.

View Replies View Related

Program That Will Allow Programs Only At Certain Time

Sep 25, 2014

So I am making a program that will allow programs only at a certain time and i'm not sure how I would set the time for the program.

View Replies View Related

Swing/AWT/SWT :: Simple Java GUI

Feb 4, 2015

I'm just starting to learn Java GUI. I'm trying to make a simple GUI that will update dynamically but having trouble ending the loop correctly.The Currently I've got the GUI updating and stopping, but the Test class method loop does not terminate correctly, it continues to count.

import java.awt.*; // Using AWT container and component classes
import java.awt.event.*; // Using AWT event classes and listener interfaces
import java.util.Timer;
import java.util.TimerTask;

[code]....

View Replies View Related

Simple Calculator Swing Java

Dec 13, 2014

I am a beginner with Java. I have never used SWING before but I have to use it now and I am clueless. I don't know if what I am doing is right.I need to create a simple calculator. My problem is getting the buttons the user clicks to appear in a text field. I know I haven't added the =/*- buttons yet.

I have left actionPerformed practically blank, what to put in ?

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
* Created by IntelliJ IDEA.
* Date: 11/12/2014
* Changing Face Program.
*/
public class Calc extends JFrame implements ActionListener

[code]...

View Replies View Related

Download Pic From URL - Simple Java I/O Code

Nov 24, 2014

I got a very simple code:

InputStream inputStream = null;
OutputStream outputStream = null;
try {
URL url = new URL(imgfm);
inputStream = url.openStream();
outputStream = new FileOutputStream(imgto);
 byte[] buffer = new byte[2048];
int length;

[Code] ....

I have several pic to download (e.g 30) however, the code work for some pic (workable for a random number of pic, sometimes workable for 10 pics but sometime can only download 1 pics).

I try to input several println code to found out the problem. finally i found that all the code stop (it's stop as when i use debug process in eclipse, it get no respond) and the final appear code under console is:

2048
Start abc/testing.jpg
Finished
2048
Start abc/testing.jpg
Finished
2048
Start abc/testing.jpg
Finished
1935
Start abc/testing.jpg
Finished

It cant even go to "System.out.println("Can come to here!");"

View Replies View Related

How To Make Simple Java Application Installable

Sep 9, 2014

I want to create simple java application which has 3 text fields and the application will have database connection. Once done, I want to run this application on other systems without using any IDE. So how do I do this? How can I make an application installable on other system so that on clicking on it, it will start executing?

View Replies View Related

Java Simple While Loop Wage Calculator

Nov 13, 2014

I need to do a simple while loop pay calculator. I am very new and not sure what I need to do to put all the requirements in the new code from another code. Here is my code that doesn't work yet.

1.Place the wage calculation routines in a while loop.
2.Allow the user to input an individual's hours worked.
3.Accumulate the total regular hours worked, the overtime hours worked, the amount of the regular pay, the amount of overtime pay, and the total payroll, for all of the employees entered.
4.Allow the user to enter a zero (0) as the Boolean expression to end the loop.
5.When the loop ends print out all of the accumulated totals with appropriate labels, each on a separate line.

import java.util.Scanner;
public class WhileLoopPayCalc
{
public static void main(String [] args) {
final double REGULAR_HOURS = 40f;
final double HOURLY_WAGE = 12.5f;
final double OVERTIME = 1.5f;
double wage = 0f;

[Code] ....

View Replies View Related

Making Executable From Simple Java Code

Feb 16, 2014

I am totally new to java. How to convert the below java code to an executable file?

var intInterval = 0
function importdata(){
filename = "C:1.txt";
AmiBroker1 = new ActiveXObject("Broker.Application" );
AmiBroker1.Import( 0, filename, "custom.format" );
AmiBroker1.RefreshAll();
}

It simply imports numerical data from 1.txt into an application amibroker. Also this import function has to be in a loop of 60 seconds.

View Replies View Related

Java Regular Expressions - Simple Search And Replace

Mar 1, 2014

I want to do a simple search and replace regular expression of lines. I am very unfamilar with Java regular expressions, and I'm not sure how to do something as simple as what I want to do. I have lines that look like this...

Java Code : access_log /home/%USER%/access_log mh_sh_highlight_all('java');

I want them changed so %USER% changes to a string, such as "cyrus," so they appear like this ...

Java Code: access_log /home/cyrus/access_log mh_sh_highlight_all('java');

The reason I want to use regular expressions is because I want to use the replaceAll method of the java.lang.String object. If I use replace I have to convert my strings into char arrays, and my code becomes bulky.

View Replies View Related

Simple Java Game - Moving Icon That Is Controlled Using Keyboard

Apr 1, 2014

I am writing code for a simple java game. The problem I am running into is that I have a moving icon (J button) that is controlled using the keyboard (arrows) and I also have stationary icons (J button). Is there a way to keep the moving icon from passing over the stationary icon? I would like the stationary icon to act like a wall.

View Replies View Related

Simple Program In Java (Eclipse) That Converts Fahrenheit To Celsius

Apr 10, 2014

I've made a simple program in Java (Eclipse) that converts Fahrenheit to Celsius.The class is:

Java Code: package ehu.student;
public class ConversorTemperaturas {
/*--------------------------------------------------------------*/
/*Clase que dado una temperatura en Fahrenheit la canvierte en C*/
/*--------------------------------------------------------------*/
public float farenheit2celsius (float d){
float celsius, aux;

[code]...

When I write t.farenheit2celsius(25.6), appears the following error: "The method farenheit2celsisus(float) in the type ConversorTemperaturas is not applicable for the arguments (double)"If i change in the class the type float for double there is not problem. Why can't i use the type float?

View Replies View Related







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