in reply to Re: Beginner's Object Orientation Exercises?
in thread Beginner's Object Orientation Exercises?
I made a Flashlight program.
It has everything but the battery power, which will be easy to implement in a few minutes when I find time.#! Perl use vars qw($on $pwrlvl); use strict; my $on = "no"; my $pwrlvl = "3"; my $fl = "Flashlight"; print "You are holding a flashlight. What will you do? \n"; print "(TurnOn, TurnOff, Flash, ChangePowerLevel, ExitProgram)\n"; print "/Status Checks/=\\ pwrlvl, ison \\\n"; while(<STDIN>) { chomp; if (/^Change/) { print "Enter Level: "; chomp (my $lvl=<STDIN>); print $fl->ChangePowerLevel("$lvl"); print "\n"; } elsif (/^(exit|q|end|x|quit)/i) { exit; } else { print $fl->$_ ."\n"; } } { #1 package Flashlight; sub TurnOn { $on = "yes"; return "The light is on."; } sub TurnOff { $on = "no"; return "Light is now off."; } sub Flash { if ($on eq "no") { return "A flash of light beams through the air!"; } else { return "The flashlight is on already."; } } sub ChangePowerLevel { my $class = shift; my $level = shift; $pwrlvl = $level; return "The Power level is set at $pwrlvl"; } sub pwrlvl { return "Power is at $pwrlvl"; } sub ison { return "Is it on? Well, $on"; } }
Commands: ison, pwrlvl, ChangePowerLevel, TurnOn, TurnOff, Flash
> munchie, the number munchin newb
Llama: The other other white meat!
(you had to be there :-P)
|
---|
Replies are listed 'Best First'. | |
---|---|
(jeffa) 3Re: Beginner's Object Orientation Exercises?
by jeffa (Bishop) on Mar 18, 2002 at 04:11 UTC | |
Re: Re: Re: Beginner's Object Orientation Exercises?
by nop (Hermit) on Mar 18, 2002 at 03:04 UTC | |
Re: Re: Re: Beginner's Object Orientation Exercises?
by Juerd (Abbot) on Mar 18, 2002 at 06:50 UTC | |
by munchie (Monk) on Mar 19, 2002 at 21:43 UTC |
In Section
Seekers of Perl Wisdom