Wikipedia:Reference desk/Archives/Computing/2008 May 18

Computing desk
< May 17 << Apr | May | Jun >> May 19 >
Welcome to the Wikipedia Computing Reference Desk Archives
The page you are currently viewing is an archive page. While you can leave answers for any questions shown below, please ask new questions on one of the current reference desk pages.


May 18

edit

MS-DOS Windows Command Prompt

edit

When using DOS command prompt, does prefixing a command with "@" in a batch file makes the line execute without displaying it on the screen? For example, a .BAT file that says:

@cd a:\

md a:\test

When executed in DOS the command prompt, would display (right?)

a:\>md a:\test

Yes. Also, the command echo off will stop commands from being displayed. Thus, you can put @echo off as the first command in a batch file to avoid having to type the @ on every line. --Bavi H (talk) 07:05, 18 May 2008 (UTC)[reply]


Also, how would I get a batch file containing something like this (below) to execute properly?

cd c:\

md c:\test

cd c:\test

When I write a batch file to create a directory and then change to that just-created directory, it won't change. There won't be an error or anything and it displays the commands correctly, it just won't execute the "cd" command. It shows the command line containing it; but after execution of the file, I am still in "c:\" directory instead of the "c:\test" directory.

Thanks much, Zrs_12

When I tested it on my computer, the batch file does indeed change the current directory to c:\test, but only if you start it from the C drive. If you start it from a:\, for example, the current directory will show a:\ at the end. This is because DOS remembers the current directory separately for each drive. Try the command c: to change the current drive to C. --Bavi H (talk) 07:05, 18 May 2008 (UTC)[reply]
If you use Windows Command Prompt (on Win XP/Vista), you can use cd /d C:\test, but it's not available on pure DOS. --grawity 12:13, 18 May 2008 (UTC)[reply]
Hmm, it's working now. I don't know what was wrong before. Thanks, Zrs 12 (talk) 15:30, 18 May 2008 (UTC)[reply]
Note that the Windows command prompt (on NT/2000/XP/Vista) is not DOS at all, pure or otherwise. It's just a Windows application that mimics some of the syntax and behavior of the old DOS command prompt. -- BenRG (talk) 17:32, 18 May 2008 (UTC)[reply]
I thought it's just a 32-bit rewrite of the good old command.com + lots of extensions. --grawity 15:34, 19 May 2008 (UTC)[reply]

Hmm, alright. I am using Windows XP command prompt, not DOS. Zrs 12 (talk) 19:15, 18 May 2008 (UTC)[reply]

Then, use cd /d folder.
Also useful are pushd folder and popd commands.
--grawity 15:34, 19 May 2008 (UTC)[reply]

hardware problem

edit

Hi. my pc just recently refused to boot. when i try to power it on, the HD led and power led turn on (doesnt blink like it used to though).. the monitor doesnt wake (but am sure its working fine), and theres no 'beep' that usually happens before it does POST. I already tried removing/reinserting the components (hd, memory, dvd-drive, vc) but no luck.. I also find it curious why when i tried to power on w/ no memory in the slot it didnt even give a warning beep.. all the fans powerup (cpu, vc, case fan). Is my mother board dead? or is it something i missed?

Specs if it matters (amd 64 3200+,1gig ddr2, 1 sata hd, 1 ide hd, dvdrw)... Thanks in advance. 124.105.117.161 (talk) 04:52, 18 May 2008 (UTC)[reply]

Sounds like your mb or CPU yeah. That's basically what happens when you touch your mobo without grounding yourself first. 210.229.27.172 (talk) 07:35, 18 May 2008 (UTC)[reply]
thanks for the reply.. am the poster above 121.97.133.42 (talk) 11:50, 19 May 2008 (UTC)[reply]

photos in ipod touch

edit

is it 100% legal to save flickr photos in my ipod and show it to friends and relatives? Is it commercial use if I put in ipod and show to others? what other websites are 100% legal we can do like this? —Preceding unsigned comment added by 59.92.121.138 (talk) 05:34, 18 May 2008 (UTC)[reply]

If you're just showing them to friends and relatives it would, in the US anyway, easily fall under fair use and so it isn't worth worrying about. This sort of "personal use" that doesn't involve anything that would cut people out of profit is usually fair use. This is independent of however the photos are licensed. --66.180.186.198 (talk) 10:25, 18 May 2008 (UTC)[reply]
Some flickr photos are Creative Commons licensed (search here). Alternatively, there's Wikimedia Commons. --h2g2bob (talk) 22:23, 18 May 2008 (UTC)[reply]

CLI question?

edit

I am looking for an example of a CLI which is currently in use. Also I need to understand why it has not been recoded as an object oriented system and have example of a device or system which uses that CLI. In addition I would like to know how and why it is utilized —Preceding unsigned comment added by 24.170.143.225 (talk) 15:40, 18 May 2008 (UTC)[reply]

We don't do homework, but you should check out Command Line Interface, which should answer all your questions. Come back and ask if there's anything you don't get in the article. Also, object-oriented doesn't mean GUI, even though GUIs generally are quite object-oriented. --Sean 16:05, 18 May 2008 (UTC)[reply]

CD Audio, 700MB = 80 minutes. Why?

edit

"2 channels of PCM audio, each signed 16-bit values sampled at 44100 Hz".

16 bit = 2 bytes.

There are 2 channels so each second takes: 2 * 2 * 44,100 bytes.

80 minutes = 80 * 60 * (2 * 2 * 44,100) bytes = 846,720,000 bytes = 807.495117 MB > 700MB.

As far as I know CD Audio standard use no compression at all. —Preceding unsigned comment added by 89.0.235.64 (talk) 17:20, 18 May 2008 (UTC)[reply]

An 80 minute CD does hold about 800 MB of audio. This is divided into about 360,000 sectors of 2352 bytes each. On a CD-ROM, usually only 2048 of those 2352 bytes are used for user data. See CD-ROM format. It was done that way for backward compatibility reasons. A significant part of the extra capacity of a DVD comes from just using a better modulation scheme. I think the DVD scheme would fit about a gigabyte of data on an ordinary CD. A simple firmware upgrade would allow any CD-ROM reader/burner to read/burn such discs, but there was never sufficient interest after DVD took off. -- BenRG (talk) 17:53, 18 May 2008 (UTC)[reply]
The last time this question was asked, I calculated the math as follows.
There are more error correction codes used in CD-ROM format than Audio CD format. According to Compact Disc#Data structure, each sector can have 2352 bytes of music in Audio CD format or 2048 bytes of data in CD-ROM format. So 846720000 bytes (80 minutes) of Audio CD music use the same number of sectors as 737280000 bytes (~703 megabytes) of CD-ROM data.
--Bavi H (talk) 02:27, 19 May 2008 (UTC)[reply]

Java: Method overloading

edit

Suppose I have a Java class called Animal and a bunch of subclasses of Animal called Tiger, Crocodile, Monkey, and so on. I also have a Java class called Zookeeper. The Zookeeper class looks something like this:

public class Zookeeper {

    public void putInCage(Animal a) {
        GenericCage cage = new GenericCage();
        cage.insert(a);
    }

    public void putInCage(Tiger t) {
        t.sedate();
        TigerCage cage = new TigerCage();
        cage.insert(t);
    }

    public void putInCage(Crocodile c) {
        c.addMuzzle();
        CrocodileCage cage = new CrocodileCage();
        cage.insert(c);
        c.removeMuzzle();
    }

    public void putInCage(Monkey m) {
        MonkeyCage cage = new MonkeyCage();
        cage.insert(m);
        m.feed(new Banana());
    }

    // etc.

}

Now, somewhere else in my code I am given an Object. This may or may not be an Animal (and if it's an Animal, it might be a Tiger or a Crocodile or a Monkey or what have you, or it might just be a plain old Animal). If it is an Animal, then I want to have my zookeeper put it in a proper cage. So I tried something like this:

public void process(Object o) {
    if (o instanceof Animal) {
        zookeeper.putInCage( (Animal) o );
    }
}

I have to cast o to an Animal, because the zookeeper doesn't have a putInCage(Object) method. The problem is that this casting always causes the generic putInCage(Animal) method to be called, even if o is actually a Tiger or a Crocodile or a Monkey, so these animals are put into cages incorrectly.

I don't want to have to include a whole bunch of lines like if (o instanceof Tiger), because there are too many subclasses of Animal, and more might be added in the future—and anyway it seems that I shouldn't have to do that (isn't that what method overloading is for in the first place?). How do I get around this problem? —Bkell (talk) 23:41, 18 May 2008 (UTC)[reply]

Overloading is resolved at compile time. Even though those methods have the same name, they have different signatures and are unrelated. The method with the argument type of "Animal" is selected at compile time because the expression (Animal) o has type Animal. You have to explain what you are doing with these "cages" in more detail, because from the code you've posted, it appears that you create a new kind of cage, and then discard it, in each method, so it's not doing anything; I'm assuming that this is not your complete code. What is the difference between these "cages"? And how are these "cages" used outside of the method? From what you are saying about not wanting the Zookeeper keep track of the types of subclasses, another possibility, depending on what you want to do, might be to have a method in Animal that is executed when it is to be put in a cage. Then subclasses of Animal can override the method and implement custom behavior. Then each type of animal would have to contain the intelligence about what is to be done to it. Is that what you want? --Spoon! (talk) 00:47, 19 May 2008 (UTC)[reply]
Yes, the specifics here are obviously not my real code (I don't really have animals and cages and zookeepers). Ignore those details; they're not relevant. The important thing is that different methods should be called depending on the type of the argument.
Overloading isn't resolved at compile time. For example, I can say
Animal a = new Tiger();
zookeeper.putInCage(a);
and the zookeeper will put the tiger in the cage properly, despite the fact that the tiger is being held in a variable declared to be of type Animal. (Try this if you don't believe me.) The same thing works if I wrap it in a method:
public void process(Animal a) {
    zookeeper.putInCage(a);
}
As far as I can tell the only reason for the failure of the code that handles Objects is because I have to explicitly cast the Object to an Animal to be able to call putInCage() at all, but the compiler interprets this as a request to use putInCage(Animal) regardless of the actual type of the animal.
For my simple example here, the better solution would be (as you say) to have the subclasses of Animal do the putting-in-cages themselves. In my actual code things are more complicated, and in fact the zookeeper himself is an Animal (and how the put-in-cage operation should be done depends on the type of the zookeeper as well as the type of the animal), so this solution doesn't work—there is no "other" class to which the methods can be moved (or, to think about it another way, if the methods are moved to the individual subclasses of Animal, then the arguments change to reflect different types of Zookeeper, so you haven't gained anything). I can explain the whole situation in great detail if you like, but I think it would needlessly introduce a whole bunch of complexity. I tried to simplify the situation as much as I could, so if you can just take it on faith that it doesn't make sense to move the methods out of the Zookeeper class. —Bkell (talk) 01:03, 19 May 2008 (UTC)[reply]
Hmm, I may have to eat my words. I could have sworn I tried
Animal a = new Tiger();
zookeeper.putInCage(a);
and it worked, but now I am trying it and it most certainly is not working: it's the putInCage(Animal) method which is being called, not the putInCage(Tiger) method. It seems I will have to concede that overloading is done at compile time, as you said, Spoon. I think that means I need to go ponder my new perspective on the Java method calling mechanism. —Bkell (talk) 01:23, 19 May 2008 (UTC)[reply]

"Overloading considered harmful" seems to address a lot of my misunderstandings. I'll read through it carefully and then maybe I can formulate a better model of my problem. —Bkell (talk) 02:00, 19 May 2008 (UTC)[reply]

  • Looks like you've found everything out, anyway. But in case it helps - what you're asking to do is dynamic casting. It can be done in C++, but it wasn't handled in Java. Java 1.5 and 6 get close with generics and the getClass() method, but while getClass() will find out what the subclass is, it still won't allow you to cast to the subclass (in the sense that it does, but the casting is determined at compilation, not runtime, so it isn't dynamic). To be honest, I'm not sure how much good it would do for you anyway, as I would assume that the aim is to create methods for Monkey, Crocodile, Tiger and so on, but if it is none of those default to Animal. Given that you'll need to create the methods anyway, the ugly "instanceof" conditionals would make sense. :) - Bilby (talk) 02:23, 19 May 2008 (UTC)[reply]
What kind of dynamic casting are you talking about? Your mention of generics makes me think you mean templates, but neither templates nor generics will solve this problem—they're statically bound just like overloaded methods. What you really need here (and what the original poster thought he had) is multiple dispatch, which neither C++ nor Java supports. The only nice way to solve this problem in C++ or Java is to put the methods in the Animal class (as Aaron Rotenberg says below, though this has nothing to do with any extra complication in C++). -- BenRG (talk) 11:20, 19 May 2008 (UTC)[reply]
Sorry, by generics I'm referring to Java 1.5's approach, where you have "unknown" class types which are instantiated by the compiler. So templates are probably right in terms of C++. I agree that it doesn't solve the problem - using getClass() and Class.cast(Object) is close (and possible because of the use of generics), but as it is statically bound it was never really in the running. (It still turns up here and there as people try it as a solution, though, which is part of why I mentioned it). I agree with you, Spoon and Aaron that overriding would be much better than overloading. But Bkell mentioned that the methods had to stay in Zookeeper, rather than being transfered and overridden to Animal, as different Zookeepers would have different approaches. Thus the ugly instanceof is one of only two solutions I can think of. The other is perhaps uglier. Add a method public void cage(Zookeeper z) { z.putInCage(this); } into Animal and each subclass of Animal (it needs to be overridden) then call it from Zookeeper with (Animal) o.cage(this);. It avoids casting, so it sidesteps the problem. But not in an elegant way. :) - Bilby (talk) 11:57, 19 May 2008 (UTC)[reply]

(edit conflict) But, as we all know, casting (not to mention everything else) in C++ is way too complicated. The standard Java pattern is, as Spoon said, to use overriding:

public class Animal {
    public void putInCage() {
        GenericCage cage = new GenericCage();
        cage.insert(this);
    }
}

public class Tiger extends Animal {
    @Override
    public void putInCage() {
        sedate();
        TigerCage cage = new TigerCage();
        cage.insert(this);
    }
}

// etc...

Then you can completely eliminate the Zookeeper class, or at least the Zookeeper.putInCage() methods. It's much prettier, and it allows clients to create their own putInCage() methods for their classes. « Aaron Rotenberg « Talk « 02:38, 19 May 2008 (UTC)[reply]

The best solution, as has been mentioned, is to refactor (some of) the method into the Animal class hierarchy. However, if you really needed to you could have the method with the most general type test the dynamic type of its argument. This is not pretty and not very maintainable, so it should be avoided where possible:
public class Zookeeper {
   public void putInCage(Animal a) {
      if (a instanceof Tiger) {
         putInCage((Tiger)a);
      } else if (a instanceof Crocodile) {
         putInCage((Crocodile)a);
      } else if (a instanceof Monkey) {
         putInCage((Monkey)a);
      } else {
         GenericCage cage = new GenericCage();
         cage.insert(a);
      }
   }
   ...
}
Note that the way these are written, the refactoring of the logic into Animal would probably be a good candidate for a template method pattern. For example, give Animal (or an interface like Cageable implemented by Animal) three methods called prepareForCage(), createCage(), and accommodateToCage(). Then Zookeeper's logic would simply become:
public class Zookeeper {
   public void putInCage(Animal a) {
      a.prepareForCage();
      a.createCage().insert(a);
      a.accommodateToCage();
   }
}
That might be overkill, but it is worth keeping in mind where the logical flow has a lot of similarity for each and each step might vary a lot between subclasses. --Prestidigitator (talk) 16:29, 19 May 2008 (UTC)[reply]