Jump to content

My first java program!


Guest K1u

Recommended Posts

Well, s3 would contain whatever was in s1 from index 7 to index 12. This is due to the fact that "substring(int startIndex,int endIndex)" returns a String from the startIindex parameter (inclusive) to the endIndex parameter (exclusive). So essentially, it returns startIndex to endIndex-1.

For future reference: http://java.sun.com/j2se/1.3/docs/api/index.html

Link to comment
Share on other sites

Well, s3 would contain whatever was in s1 from index 7 to index 12. This is due to the fact that "substring(int startIndex,int endIndex)" returns a String from the startIindex parameter (inclusive) to the endIndex parameter (exclusive). So essentially, it returns startIndex to endIndex-1.

Lol thx i knew the answer i was just giving a random question from it lol :)

Link to comment
Share on other sites

It is bad programming to chuck declerations where ever you want.

I do not believe you are a professional developer, or that you have a ton experience (feel free to correct me if I'm wrong) to back up a statement like that (not that I do, either). You may believe that is so, as well as many other people, but I don't think it is fair to call it "bad programming". There are also many poeple who would disagree with you.(I mentioned this guy in my previous post, he does not think it is "bad programming". ).

It makes the code look ugly. And if your code is that nice that you allways have small functions that why not keep it looking nice by declaring things up the top. You say that your code isnt that big so it shouldnt be to hard to move the cursor up the top and declare it there.

This is another opinion. You may think it looks nice by having all the variables up there, but someone else may think it's just clutter.

Since the functions are small, will you really have a hard time remeber what a variable was you declared 5 lines ago? Or is it all that hard to take 5 seconds to scan through the previous 15 lines compared to 2 seconds to go straight to the top of the funciton?

If I'm using a variable once inside of some nested loops or if statements, why should I delcare it at the top of the function? This is just needlessly extending it's scope. Or if I need a temporary variable at the end of the function why declare it way at the top when I'm only using it 2 statements before I return?

I agree that if you're developing software with a group of people it's obviously a bad idea to use a different coding style than the rest of them.

But I don't agree that it is "bad programming" and should not be used.

I think this topic is more of an opinion than some sort of coding rule.

Link to comment
Share on other sites

In that case why is it from every single tutorial i have read they said never to do that, all through high school my teacher's said never to do that and would mark you down for it. All through uni you are told not to do that and would lose alot of marks if you done it. I asked if you meant something like in a for loop and from the answers i got i assumed you didnt mean something like inside a forloop or if statment, inside them then yeh sure as it can be seen as its own block of code. A temp ver that is just holding a peive of data for you for a few secounds sure that could be fine. But not something that you just didnt realize you will need so you chuck the decleration in the spot that you realize you needed it, i would have to say that is bad programming practice imo

Link to comment
Share on other sites

Seriously Spider, chill, you don't need to hate people just because they have a different opinion. Also, I didn't even disagree with you on the placement of the declarations and still you call what I say bullshit. Putting declarations at the beginning of blocks is often the recommended way of doing things and it is how I do it except in very special cases.

The official code conventions for Java

I just know from experience that discussing how to format code is one of the worst wastes of time you can have in a larger project. For instance, which of the following ways of placing the brace is the correct way?

int doSomething1()

{

}



int doSomething2(){

}



int doSomething3() {

}

The answer: The one the group can agree on, which hopefully is the same as the code convention for the language but it doesn't have to be.

However, writing long and complex methods where you could actually get lost and not understand how it works is basically the definition of bad programming. It is bad since it is completely unmaintainable and the risk of introducing severe bugs increase greatly. If you were calling that bullshit then you really don't know what you are talking about. I know there are rare occasions where an API might force you to write long and unreadable code but for the most part it is just the result of bad programmers being lazy.

Link to comment
Share on other sites

Heres what i made for class friday pretty simple program...

//********************************************************

// Generates 2 random dice rolls and gives total of both.

//

// Author: Kiumarz Hashemi

//********************************************************



import java.util.Random;



public class Dice

{

    public static void main (String[] args)

    {

     Random generator = new Random();

     

     

     int dice1;

     int dice2;

     int total;

     

     

     dice1 = generator.nextInt(6)+ 1;

     System.out.println ("First roll t " + dice1);

     

     dice2 = generator.nextInt(2)+ 1;

     System.out.println ("Second roll t " + dice2);

     

     total = dice1 + dice2;

     System.out.println ("Total tt " + total);

     

     

        

    }

}

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...