Open Side Menu Go to the Top
Register
Programming homework and newbie help thread Programming homework and newbie help thread

04-10-2015 , 07:54 PM
Back in the day we were taught that declaring variables at the top was the right way to do it. "Right" is obviously pretty flexible.

The advantage used to be that you always knew where to look to find out what type a variable was, but with the rise of IDEs, if I want to know what type a variable is I mouse over it. The advantages of declaring close to use are:

- I can tell that a variable hasn't been used further up function, so I don't have to worry about some residual value being left in it.

- If the code in the function changes and the variable is no longer required, I'm not left with a pointless declaration higher up in the function that I have to go seek out and destroy.

- Less lines of code means more I can fit on my screen at once to see what's going on.

- Clear scoping (see next post)

In ReSharper, which is a refactoring/style/IDE usability helper for C#, one of the things it does for you automatically is move variable declarations as close as possible to usage. So even though it's not really that important, you should probably follow that rule as that does tend to be the standard nowadays.

Last edited by ChrisV; 04-10-2015 at 08:00 PM.
Programming homework and newbie help thread Quote
04-10-2015 , 07:59 PM
Quote:
Originally Posted by Anais
i agree, but then he has to reinitialize 'i' as an int in each for loop

probably bad practice to do it the way he's doing it, but not the end of the world while starting out
It's better to instantiate each loop. There's no performance hit with modern compilers. Instantiating each loop means it's clear what the scope of the variable is. If I saw code done the way NiSash has done it, I would assume the value of the variable at the end of the loop is being used down function for some purpose. Before say messing with the loop I'd have to read down function or do a Find Usages to ensure that wasn't the case.
Programming homework and newbie help thread Quote
04-10-2015 , 08:41 PM
That's a really good point.
Programming homework and newbie help thread Quote
04-11-2015 , 03:22 AM
Quote:
Originally Posted by POW
Basic Netbeans GUI question

Hi guys, Ive created a Jframe, added a JPanel, and overridden the paintComponent() method:

public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.black);
g.drawRect(100, 100, 100, 100);
}


However the constructor of the JPanel never seems to invoke the paintComponent() method:

public TablePanel() {
initComponents();
}



In all the examples I've found online paintComponent(g) seems to be invoke automatically from the constructor. Any idea what I'm doing wrong?

Thanks alot, been stuck on this over an hour and going crazy!
bump
Programming homework and newbie help thread Quote
04-11-2015 , 08:39 AM
Quote:
Originally Posted by NiSash1337
Hey guys, beein trying to learn coding conventions for java and read about that variable decleration should be as close to the first use as possible:

Code:
int a;
int b;
int c;
int i;

for (i = 0; i <=10; i++)
{
     a = 2 + i;
}

for (i = 0; i <=10; i++)
{
     b = 3 + i;
}

for (i = 0; i <=10; i++)
{
     c = 4 + i;
}
would be the current way I code, but I read that the right way to do it would be:

Code:
int a;
int i;

for (i = 0; i <=10; i++)
{
     a = 2 + i;
}

int b;

for (i = 0; i <=10; i++)
{
     b = 3 + i;
}

int c;

for (i = 0; i <=10; i++)
{
     c = 4 + i;
}
would be the correct way to do it?
If a, b, and c are closely related in some way, I might declare them together. Without context, though, I definitely prefer the second way. I would also remove the whitespace between the variables and their respective loops to make the visual grouping more clear.

Code:
int a;
for (int i = 0; i <= 10; i++)
{
     a = 2 + i;
}

int b;
for (int i = 0; i <= 10; i++)
{
     b = 3 + i;
}

int c;
for (int i = 0; i <= 10; i++)
{
     c = 4 + i;
}
Programming homework and newbie help thread Quote
04-11-2015 , 10:21 AM
Ok thx another question, I'm working on a program that reads out a txtfile with forexdata. So far I got everything working but the program is growing by a lot and it's all in one class at the moment.

With what criteria should I use a new class? Right now I'm thinking that I should use one class for the preparation of the txtfile(Some data has to be thrown out and replaced).

Then one class to read out the data from the txtfile and write it in the lists.

And another class to analyze the data from the lists.

Is that ok or is there like a convention about this as well?
Programming homework and newbie help thread Quote
04-20-2015 , 10:25 AM
Hey guys, I'm currently doing the Odin Project in my spare time, currently on the 2nd course where you build the google home page. I've made some small simple websites before, however right now I seem to be having a problem getting CSS to work properly on my html layout.

My current head looks as follows;

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="/normalize.css">
<link type="text/css" rel="stylesheet" href="/style.css">
<title>Google</title>
</head>

The css sheet called "style.css" is in the same folder as the html project file. If there is anything wrong with this code, some help would be greatly appreciated. Thanks guys!
Programming homework and newbie help thread Quote
04-20-2015 , 11:43 AM
Try it sans the / ?
Programming homework and newbie help thread Quote
04-20-2015 , 11:53 AM
Quote:
Originally Posted by Anais
Try it sans the / ?
Tried, still unresponsive
Programming homework and newbie help thread Quote
04-20-2015 , 11:57 AM
can you maybe post in [ code ] tags the code for both files? or all three, depending on what normalize.css is
Programming homework and newbie help thread Quote
04-20-2015 , 12:14 PM
Are there specific styles you're setting in the styles.css file that aren't getting applied? Your post sounds like that is the case. When working with css, you need to keep in mind specificity. That will determine which css rules get applied and which don't.
Programming homework and newbie help thread Quote
04-20-2015 , 03:00 PM
Quote:
Originally Posted by Tilltard
Hey guys, I'm currently doing the Odin Project in my spare time, currently on the 2nd course where you build the google home page. I've made some small simple websites before, however right now I seem to be having a problem getting CSS to work properly on my html layout.

My current head looks as follows;

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="/normalize.css">
<link type="text/css" rel="stylesheet" href="/style.css">
<title>Google</title>
</head>

The css sheet called "style.css" is in the same folder as the html project file. If there is anything wrong with this code, some help would be greatly appreciated. Thanks guys!
Hi I am doing TOP as well. Not very advanced so I am not really sure what all your code is doing/trying to do. Mine is much simpler.

<!DOCTYPE html>
<html>
<head>
<title>Google clone</title>
<link rel="stylesheet" href="stylesheet.css">
</head>

On another note, does anyone have some tips for learning jquery? I don't like either codeschool or codecademy which TOP recommends.
Programming homework and newbie help thread Quote
04-20-2015 , 04:23 PM
ran thru TOP's rebuild google project, here's all what i did. it's not perfect by any means, but it looks fairly similar (didn't bother with turning the bottom rows into links, the theory is the same as the top)

stylesheet in the same folder as the html file of course

Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link type="text/css" rel="stylesheet" href="style.css">
<title>Google</title>
</head>
<body>
	<nav>
		<ul>
				<li><a href="https://plus.google.com/">+You</a></li>
				<li><a href="https://mail.google.com/">Gmail</a></li>
				<li><a href="https://images.google.com/">Images</a></li>
				<li>Your Email</li>
		</ul>
	</nav>
	<div><img src="https://www.google.com/images/srpr/logo11w.png"></div>
	<form>
		<input type="text" name="search" size="50">
	</form>
	<br>
	<div id="b1">
		Google Search
	</div>
	<div id="b2">
		I'm Feeling Lucky
	</div>
	<footer>
		<ul style="float:left;">
			<li>Advertising</li>
			<li>Business</li>
			<li>About</li>
		</ul>
		<ul style="float:right; padding-right: 25px;">
			<li>Privacy</li>
			<li>Terms</li>
			<li>Settings</li>
		</ul>
	</footer>
</body>
</html>
Code:
/* * {
	border: 1px solid black;
} */

body {
	font-family: sans-serif;
	font-size: .8em;
}

li {
list-style-type: none;
display: inline;
padding: 5px;
}

nav {
	position: auto;
	margin-left: 75%;
	text-alight: center;
}

a{
	text-decoration: none;
	color: black;
}

a:hover {
	text-decoration: underline;
}

img {
	height: 25%;
	width: 25%;
	position: relative;
	left: 35%;
	padding-top: 160px;
}

form {
	margin-left: 33%;
	width: 50%;
	padding-top: 20px;
	
}

footer {
	clear: both;
	position: fixed;  
	bottom: 0px;
	background-color: #e3e3e3;
	width: 100%;
	padding-left: 5px;
	margin-left: -10px;
	height: 30px;
}

footer ul {
	display: inline;
}

#b1, #b2 {
	border: 1px solid black;
	background-color: #e3e3e3;
	border-radius: 3px;
	height: 15px;
	padding: 2px
}

#b1:hover, #b2:hover{
	box-shadow: 0 0 2px #000000;
}

#b1 {
	margin: auto;
	margin-left: 40%;
	float: left;
	width: 90px;
}

#b2 {
	margin: auto;
	margin-left: 50%;
	width: 110px;
}
Very reminded of how much i hate html/css
Programming homework and newbie help thread Quote
04-20-2015 , 04:26 PM
Hi all! suppose if i have a simple C function(see below) which generates the nth Fibonacci number and I want to prove its correctness(using frama C), what are the necessary pre and post conditions? and what should I choose as my loop invariant(s?)
Code:
code:
int fib(int n)
{
	int previous = 1;
	int previous2 = 0;
	int i = 2;
	int temp;
	if (n==0) return 0;
	if (n==1) return 1;
	while (i <= n)
	{
		temp = previous + previous2;
		previous2 = previous;
		previous = temp;
                i++;
	}
	return previous;
}
I fail to see any preconditions other than n>=0 and fib(0) = 0, fib(1) =1 (do these even count as preconditions?)
and should I choose my loop invariant to be at the end of each iteration previous = fib(n-1) +fib(n-2) (i'm not sure if i am allowed to use the function itself in the loop invariant)
and what should the post conditions be?? other than the fact that it should return the nth Fibonacci number?

appreciate all input =)
Programming homework and newbie help thread Quote
04-20-2015 , 05:08 PM
Quote:
Originally Posted by wishingwell
Hi I am doing TOP as well. Not very advanced so I am not really sure what all your code is doing/trying to do. Mine is much simpler.

<!DOCTYPE html>
<html>
<head>
<title>Google clone</title>
<link rel="stylesheet" href="stylesheet.css">
</head>

On another note, does anyone have some tips for learning jquery? I don't like either codeschool or codecademy which TOP recommends.
Quote:
Originally Posted by Anais
ran thru TOP's rebuild google project, here's all what i did. it's not perfect by any means, but it looks fairly similar (didn't bother with turning the bottom rows into links, the theory is the same as the top)

stylesheet in the same folder as the html file of course

Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link type="text/css" rel="stylesheet" href="style.css">
<title>Google</title>
</head>
<body>
	<nav>
		<ul>
				<li><a href="https://plus.google.com/">+You</a></li>
				<li><a href="https://mail.google.com/">Gmail</a></li>
				<li><a href="https://images.google.com/">Images</a></li>
				<li>Your Email</li>
		</ul>
	</nav>
	<div><img src="https://www.google.com/images/srpr/logo11w.png"></div>
	<form>
		<input type="text" name="search" size="50">
	</form>
	<br>
	<div id="b1">
		Google Search
	</div>
	<div id="b2">
		I'm Feeling Lucky
	</div>
	<footer>
		<ul style="float:left;">
			<li>Advertising</li>
			<li>Business</li>
			<li>About</li>
		</ul>
		<ul style="float:right; padding-right: 25px;">
			<li>Privacy</li>
			<li>Terms</li>
			<li>Settings</li>
		</ul>
	</footer>
</body>
</html>
Code:
/* * {
	border: 1px solid black;
} */

body {
	font-family: sans-serif;
	font-size: .8em;
}

li {
list-style-type: none;
display: inline;
padding: 5px;
}

nav {
	position: auto;
	margin-left: 75%;
	text-alight: center;
}

a{
	text-decoration: none;
	color: black;
}

a:hover {
	text-decoration: underline;
}

img {
	height: 25%;
	width: 25%;
	position: relative;
	left: 35%;
	padding-top: 160px;
}

form {
	margin-left: 33%;
	width: 50%;
	padding-top: 20px;
	
}

footer {
	clear: both;
	position: fixed;  
	bottom: 0px;
	background-color: #e3e3e3;
	width: 100%;
	padding-left: 5px;
	margin-left: -10px;
	height: 30px;
}

footer ul {
	display: inline;
}

#b1, #b2 {
	border: 1px solid black;
	background-color: #e3e3e3;
	border-radius: 3px;
	height: 15px;
	padding: 2px
}

#b1:hover, #b2:hover{
	box-shadow: 0 0 2px #000000;
}

#b1 {
	margin: auto;
	margin-left: 40%;
	float: left;
	width: 90px;
}

#b2 {
	margin: auto;
	margin-left: 50%;
	width: 110px;
}
Very reminded of how much i hate html/css
Haha, I just finished my HTML but the CSS is not being responsive at all, so I really haven't been able to start it.

Long code here, apologies in advance;

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="normalize.css">
<link type="text/css" rel="stylesheet" href="googlestyle.css">
<Title>Google</title>
</head>

<body>
<div id="container">
<div id="headbar">
<ul id="header">
<li><a href="https://plus.google.com/?gpsrc=ogpy0&tab=wX">+You</a></li>
<li><a href="https://mail.google.com/mail/?tab=wm">Gmail</a></li>
<li><a href="https://www.google.ca/imghp?hl=e&tab=wi&ei=ahAjVd2VKYutyASqh4GADg&ved=0C BIQqi4oAg">Images</a></li>
<li><a href="http://www.google.ca/intl/en/options/">Apps</a></li>
<li><a href="https://accounts.google.com/ServiceLogin?hl=en&continue="><button type="button">Sign In</button></a></li>
</ul>
</div>
<div id="logo">
<img src="https://www.google.ca/images/srpr/logo11w.png">
</div>
<div id="searchbar">
<form>
<input type="text" id="search" name="search_box" action="http://www.google.ca">
</form>
</div>
<div id="buttons">
<button type="button">Google Search</button>
<button type="button">I'm Feeling Lucky</button>
</div>
<div id="french">
<p> Google.ca offered in: <a href="https://www.google.ca/setprefs?sig=0_zbQT8WTA_CwCB92jvNS4cwSGINE%3D&hl=f r&source=homepage">Français</a></p>
</div>
</div id="footer">
<ul id="leftfoot">
<li><a href="https://www.google.ca/intl/en/ads/?fg=1">Advertising</a></li>
<li><a href="https://www.google.ca/services/?fg=1">Business</a></li>
<li><a href="https://www.google.ca/intl/en/about.html?fg=1">About</a></li>
</ul>
<ul id="rightfoot">
<li><a href="https://www.google.ca/intl/en/policies/privacy/?fg=1">Privacy</a></li>
<li><a href="https://www.google.ca/intl/en/policies/terms/?fg=1">Terms</a></li>
<li><a href="https://www.google.ca/preferences?hl=en">Settings</a></li>
</ul>
</div>
</div>
</body>
</html>
Programming homework and newbie help thread Quote
04-20-2015 , 06:24 PM
I copied your html into notepad++ and tried it out on my comp. Then I went ahead and created a css document called "googlestyle.css" and used that to style your html document. It works fine, no issues. Do something really simple and see if it works. For example in your css sheet do:

body {
background-color: #b0c4de;
}

Your background color should change. If it works then great, your issues are likely in your css code. If not.

1. make sure your css file is actually called "googlestyle.css" Make sure you are saving it as a css file.
2. make sure both your html and your css file are saved in the same folder.
Programming homework and newbie help thread Quote
04-20-2015 , 07:15 PM
Quote:
I just finished my HTML but the CSS is not being responsive at all, so I really haven't been able to start it.
what exactly does this mean?

do you have separate files called "normalize.css" & "googlestyle.css"?

what browser are you using?

don't forget to wrap your posted code in [ code ] [ /code ] tags

edit

you could also try inlining your css, like:

<body style="background-color: black;">

Last edited by Anais; 04-20-2015 at 07:44 PM.
Programming homework and newbie help thread Quote
04-20-2015 , 08:38 PM
Quote:
Originally Posted by Anais
what exactly does this mean?

do you have separate files called "normalize.css" & "googlestyle.css"?

what browser are you using?

don't forget to wrap your posted code in [ code ] [ /code ] tags

edit

you could also try inlining your css, like:

<body style="background-color: black;">
Sorry, should have clarified. Yes I do have two additional files called normalize and googlestyle. The normalize is a CSS reset file I have downloaded from a tutorial I was learning from, and "googlestyle.css" is my separate css file.

Using Firefox ps, and I tried changing the background with no sucess.

Last edited by Tilltard; 04-20-2015 at 08:44 PM.
Programming homework and newbie help thread Quote
04-20-2015 , 09:08 PM
i would comment out the "normalize" file, and for the time being the "googlestyle" file as well. Either delete the lines or comment them. forget what html is for comments, something like <!-- -->
Programming homework and newbie help thread Quote
04-21-2015 , 12:38 AM
I am confused because i can make changes using css to your document. I did not change anything in your html document, so that cannot be the problem. I downloaded the normalize.css file and saved it as normalize.css in the same folder as my html and googlesheet.css file. It doesn't really seem to change anything. I can still change stuff using css.

Maybe try saving the html in a new folder. Then create a new css file named googlesheet.css that has just one very basic thing like my background color change I suggested. Are you sure that your css is actually saved as googlesheet.css ? sometimes I have saved my css file with the wrong name, something like googlsheet.css and then am wondering why it doesn't work.

If all of this fails, try to do it on another computer. Either a friend or school or something. If you still can't get it to work you are doing something wrong because it is working fine on my comp. If it works fine on another computer, then it is some issue with your computer.
Programming homework and newbie help thread Quote
04-21-2015 , 08:11 AM
if he's sure he's not saving it as "googolesheet.css.txt" (which is pretty easy to do if you're not careful), then I'd try another browser.

Well, first I'd try removing all the linked css files and just inline a bit of css code.

If all that doesn't work, then try another browser.

If that doesn't work, maybe copy/paste some code from codecademy and see if it works in one of his browsers.

Still a lot of options available.
Programming homework and newbie help thread Quote
04-21-2015 , 10:09 AM
You guys should use this to share and troubleshoot:

http://plnkr.co/
Programming homework and newbie help thread Quote
04-21-2015 , 06:51 PM
Hi all,

I'm in need of assistance with a java application.

I'm trying to build an application with a gui that takes text that've been input in a textfield (txtInput) and sends it to a web service where it gets a reply and then presents that reply in a textarea (txtDisplay). And a button to send it.

As it is it's not working, and from the below, "send" and "txt.Display" gets marked red.

final String display = control.send(input);
txtDisplay.setText(display);


Here's what I've got so far:

gui.java:

Spoiler:
Code:
package webservicefile.view;
import java.awt.EventQueue;

import javax.swing.JFrame;

import java.awt.CardLayout;

import javax.swing.JTextField;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.SwingConstants;

import java.awt.TextField;
import java.awt.GridLayout;

import javax.swing.border.TitledBorder;
import javax.swing.BoxLayout;

import webservicefile.control.Controller;
import javax.swing.JTextArea;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class window {

	private JFrame frame;
	private Controller control;
	private JTextField txtInput;
	/**
	 * Launch the application.
	 */
	

	/**
	 * Create the application.
	 */
	public window(Controller control) {
		this.control = control;
		initialize();
		frame.setVisible(true);
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(new GridLayout(1, 0, 0, 0));
		
		JPanel panel = new JPanel();
		frame.getContentPane().add(panel);
		panel.setLayout(null);
		
		txtInput = new JTextField();
		txtInput.setBounds(116, 30, 207, 28);
		panel.add(txtInput);
		txtInput.setColumns(10);
		
		JTextArea txtDisplay = new JTextArea();
		txtDisplay.setBounds(116, 133, 207, 121);
		panel.add(txtDisplay);
		
		JButton btnSend = new JButton("Send");
		btnSend.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			    final String input = txtInput.getText();
	            final String display = control.send(input);
	            txtDisplay.setText(display);
			}
		});
		btnSend.setBounds(334, 31, 96, 28);
		panel.add(btnSend);
		
	}
}


controller.java

Spoiler:
Code:
package webservicefile.control;

import java.awt.EventQueue;
import java.rmi.RemoteException;

import xx.WebServiceFile.xx;

import webservicefile.view.window;

public class Controller {
	private WebServicexx service;
	public static void main(String[] args) {
		final Controller control = new Controller();
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					window window = new window(control);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	public Controller() {
		service = new WebServicexx();
	}
	
	public String getFileContents(String filename) {
		try {
			return service.getFileContents(filename);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}


I don't really know what to do here :S

Thanks
Programming homework and newbie help thread Quote
04-22-2015 , 12:27 PM
trying project euler with javascript (as part of odin), on number two the question is:

Quote:
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
What I came up with sans recursion is:

Spoiler:
var a = 2, b, c = 0;

for(i = 1;i<4000000;i=b)
{
b = a;
if (a % 2 === 0)
{
c += a;
}
a += i;
}

console.log(c);

> 4613732


That seem about right? Any better way to do it without recursion?
Programming homework and newbie help thread Quote
04-24-2015 , 10:26 AM
Quote:
Originally Posted by Anais
trying project euler with javascript (as part of odin), on number two the question is:



What I came up with sans recursion is:

Spoiler:
var a = 2, b, c = 0;

for(i = 1;i<4000000;i=b)
{
b = a;
if (a % 2 === 0)
{
c += a;
}
a += i;
}

console.log(c);

> 4613732


That seem about right? Any better way to do it without recursion?
Newish to programming so not sure how to compare algorithm speed but the following equation is the closed form of the Fibonnaci sequence:

Fsub(n) = (((1+sqrt(5))/2)^n- ((1-sqrt(5))/2)^n/sqrt(5)

So you could loop through only even powers of N and compute that result. Like I said not sure if it's faster or better in any way.
Programming homework and newbie help thread Quote

      
m