Docsity

Simple Classes Handout - Programming Concepts-Java | CSCI 261, Exams of Computer Science

Toggle sidebar
bg1
CSCI261 Simple Classes Handout July 29, 2009
Class “Header” File:
nucleus.h
1/∗ ∗
2∗A nu c l e u s c l a s s
3∗/
4
#pragma
on ce
5
6
#include
<i os tr ea m >
7
#include
< st r i n g >
8
using namespace std
;
9
10 /∗ ∗
11 ∗nu c le u s i n t e r f a c e
12 ∗/
13
class nucleus
{
14
public
:
15 / / d e f a u l t c t o r ma kes a H+ i o n
16
nucleus
( ) ;
17
nucleus
(
int num_protons
,
int num_neutrons
) ;
18 / / t h e a t o mi c sy mb o l f o r t h e nu c l eu s
19
string symbol
( )
const
;
20 / / g et f o r pr o t on s
21
int get_protons
( )
const
;
22 / / s e t a nd ge t f o r n e ut r o n s
23
void set_neutrons
(
int newvalue
) ;
24
int get_neutrons
( )
const
;
25 / / a new n uc l e u s b as ed on t h e c a l l i n g o b j e c t b u t wi t h num
26 / / n eu tr o ns fr om nu c add ed .
27
nucleus transfer_neutrons
(
nucleus
&
nuc
,
int num
) ;
28 / / r ea d va l u e f r om t he i n p u t st r ea m −−
29 / / r e t u r n s a r e f e r e n c e t o t h e is t r e a m a rg um e nt p r o v i d e d
30
istream
&
input
(
istream
&
is
) ;
31 / / p r i n t m y s e l f t o th e o u t p u t st r e am
32 / / r e t u r n s a r e f e r e n c e to t h e o s tr e a m a rg um e nt p r o v id e d
33
ostream
&
output
(
ostream
&
os
)
const
;
34
private
:
35 / / s e t s ne u t r o ns = n an d r e t u r n s t r u e i f n >=0
36
bool alter_neutrons
(
int n
) ;
37 / / da t a memb ers
38
int protons
;
39
int neutrons
;
40 };
Class Implementation File:
nucleus.cxx
1/∗ ∗
2∗A nu c l e u s c l a s s
3∗/
4
5
#include
<i os tr ea m > / / sy st em he ad er s f i r s t
6
7
#include "nucleus. h"
/ / d ev el o pe r he ad er s s eco nd
8
#include "atomic_symbol .h"
/ / at o mi c num −> sy m bo l fu n c t i o n
9
10
using namespace std
;
11
12 /∗ ∗
13 ∗nu c l e u s im p l e m e n t a t i o n
14 ∗/
15
16 / / d e f a u l t c t o r c r e a t e s H+ by da t a m ember as s i gn m e nt
17
nucleus
: :
nucleus
( )
18 {
19
protons
= 1;
20
neutrons
= 0;
21 }
22
23 / / t h i s ct o r c r e a t e s an a r b it r a r y n u c l e u s b y
24 / / da t a membe r as si gn me nt
25
nucleus
: :
nucleus
(
int num_protons
,
int num_neutrons
)
26 {
27
if
(
num_protons
<= 0 ) {
28
cout
<<
"Error: invalid num_protons."
<<
endl
;
29 }
els e if
(
neutrons
< 0 ) {
30
cout
<<
"Error: negative neutrons."
<<
endl
;
31 }
else
{
32 / / e v e r y t h i n g ok
33
protons
=
num_protons
;
34
neutrons
=
num_neutrons
;
35
return
;
36 }
37 / / w e ge t he r e on a n e r r o r o n l y −− cr e a t e H+
38
protons
= 1;
39
neutrons
= 0;
40 }
1
pf3
pf4

Partial preview of the text

Download Simple Classes Handout - Programming Concepts-Java | CSCI 261 and more Exams Computer Science in PDF only on Docsity!

Class “Header” File: nucleus.h

2 ∗ A n u c l e u s c l a s s 3 ∗ / 4 # pragma o n c e 5 6 # include < i o s t r e a m > 7 # include < s t r i n g > 8 using namespace std ; 9 10 / ∗ ∗ 11 ∗ n u c l e u s i n t e r f a c e 12 ∗ / 13 class nucleus { 14 public : 15 / / d e f a u l t c t o r makes a H+ i o n 16 nucleus ( ) ; 17 nucleus ( int num_protons , int num_neutrons ) ; 18 / / t h e a t o m i c s y m b o l f o r t h e n u c l e u s 19 string symbol ( ) const ; 20 / / g e t f o r p r o t o n s 21 int get_protons ( ) const ; 22 / / s e t and g e t f o r n e u t r o n s 23 void set_neutrons ( int newvalue ) ; 24 int get_neutrons ( ) const ; 25 / / a new n u c l e u s bas ed on t h e c a l l i n g o b j e c t b u t w i t h num 26 / / n e u t r o n s f r o m nuc added. 27 nucleus transfer_neutr on s ( nucleus& nuc , int num ) ; 28 / / r e a d v a l u e f r o m t h e i n p u t s t r e a m −− 29 / / r e t u r n s a r e f e r e n c e t o t h e i s t r e a m a r g u m e n t p r o v i d e d 30 istream& input ( istream& is ) ; 31 / / p r i n t m y s e l f t o t h e o u t p u t s t r e a m 32 / / r e t u r n s a r e f e r e n c e t o t h e o s t r e a m a r g u m e n t p r o v i d e d 33 ostream& output ( ostream& os ) const ; 34 private : 35 / / s e t s n e u t r o n s =n and r e t u r n s t r u e i f n >= 36 bool alter_neutrons ( int n ) ; 37 / / d a t a members 38 int protons ; 39 int neutrons ; 40 } ;

Class Implementation File: nucleus.cxx

2 ∗ A n u c l e u s c l a s s 3 ∗ / 4 5 # include < i o s t r e a m > / / s y s t e m h e a d e r s f i r s t 6 7 # include " nucleus. h " / / d e v e l o p e r h e a d e r s s ec ond 8 # include " atomic_symbol. h " / / a t o m i c num − > s y m b o l f u n c t i o n 9 10 using namespace std ; 11 12 / ∗ ∗ 13 ∗ n u c l e u s i m p l e m e n t a t i o n 14 ∗ / 15 16 / / d e f a u l t c t o r c r e a t e s H+ by d a t a member a s s i g n m e n t 17 nucleus : : nucleus ( ) 18 { 19 protons = 1 ; 20 neutrons = 0 ; 21 } 22 23 / / t h i s c t o r c r e a t e s an a r b i t r a r y n u c l e u s by 24 / / d a t a member a s s i g n m e n t 25 nucleus : : nucleus ( int num_protons , int num_neutrons ) 26 { 27 if ( num_protons <= 0 ) { 28 cout << " Error : invalid num_protons. " << endl ; 29 } else if ( neutrons < 0 ) { 30 cout << " Error : negative neutrons. " << endl ; 31 } else { 32 / / e v e r y t h i n g ok 33 protons = num_protons ; 34 neutrons = num_neutrons ; 35 return ; 36 } 37 / / we g e t h e r e on an e r r o r o n l y −− c r e a t e H+ 38 protons = 1 ; 39 neutrons = 0 ; 40 }

Class Implementation File: nucleus.cxx continued...

42 string nucleus : : symbol ( ) const 43 { 44 return atomic_symbol ( protons ) ; 45 } 46 47 int nucleus : : get_protons ( ) const 48 { 49 return protons ; 50 } 51 52 int nucleus : : get_neutrons ( ) const 53 { 54 return neutrons ; 55 } 56 57 void nucleus : : set_neutrons ( int newvalue ) 58 { 59 if (! alter_neutrons ( newvalue ) ) { 60 / / ERROR! 61 cout << " Oops! Negative neutrons! " ; 62 cout << endl ; 63 } 64 } 65 66 / / c opy o f c a l l i n g o b j e c t + num n e u t r o n s f r o m nuc 67 nucleus nucleus : : transfer_neutr on s ( nucleus& nuc , 68 int num ) 69 { 70 if ( num < 0 ) { 71 cout << " Negative neutron transfer! " << endl ; 72 } else if ( nuc. alter_neutrons ( nuc. neutrons −num ) ) { 73 / / i f t h e y e x i s t r e t u r n a new n u c l e u s 74 return nucleus ( protons , neutrons + num ) ; 75 } else { 76 / / ERROR! 77 cout << " Not enough neutrons! " << endl ; 78 } 79 80 / / r e t u r n H y drogen on e r r o r 81 return nucleus ( ) ; 82 } 83 84 / / h e l p e r f u n c t i o n − s e t t h e number o f n e u t r o n s 85 bool nucleus : : alter_neutrons ( int n ) 86 { 87 if ( n >= 0 ) { 88 neutrons = n ; 89 return true ; 90 } 91 return false ; 92 } 93 94 ostream& nucleus : : output ( ostream& os ) const 95 { 96 / / Do n o t p r i n t an e n d l o r ’ \ n ’! 97 os << symbol ( ) << " " << protons << " " << neutrons ; 98 return os ; 99 } 100 101 istream& nucleus : : input ( istream& is ) 102 { 103 int p , n ; 104 string s ; 105 106 / / f o r m a t i s SYMBOL #p # n 107 if ( is >> s >> p >> n ) { 108 / / i f t h e i n p u t w ork ed , AND t h e v a l u e s a r e v a l i d 109 if ( p > 0 && n >= 0 ) { 110 protons = p ; 111 neutrons = n ; 112 } else { 113 cout << " Invalid number of protons or neutrons! " ; 114 cout << endl ; 115 / / s e t t h e i n p u t s t r e a m t o a f a i l u r e s t a t e! 116 is. setstate ( ios : : failbit ) ; 117 } 118 } 119 / / r e t u r n s i s w i t h f a i l u r e s t a t e i n t a c t 120 return is ; 121 }

Driver “main”: nucleus_main.cxx continued...

63 int main ( ) 64 { 65 / / f i l e name 66 const string hetest ( " HeliumTest. txt " ) ; 67 ofstream outfile ( hetest. c_str ( ) ) ; 68 if (! outfile ) { 69 cout << " !?! Cannot write " << hetest << endl ; 70 exit ( 1 ) ; 71 } 72 73 / / d e f a u l t c t o r y i e l d s H 74 nucleus Hydrogen ; 75 nucleus Helium ( 2 , 2 ) ; 76 77 cout << " Here is a nucleus : " ; 78 Hydrogen. output ( cout ) ; 79 cout << endl ; 80 81 cout << " And here is another : " ; 82 Helium. output ( cout ) << endl ; 83 84 / / c hange h y d r o g e n n e u t r o n s 85 Hydrogen. set_neutrons ( 2 ) ; 86 cout << " Set Hydrogen with 2 neutrons : " ; 87 Hydrogen. output ( cout ) << endl ; 88 89 test_neutron_t ra n sf er ( Helium , Hydrogen ) ; 90 91 labeled_nucle us _o ut pu t ( " New Hydrogen : " , Hydrogen , outfile ) ; 92 labeled_nucle us _o ut pu t ( " New Helium : " , Helium , outfile ) ; 93 94 / / c l o s e o u t p u t f i l e 95 outfile. close ( ) ; 96 97 / / t e s t r e a d i n g f r o m t h e n e w l y c r e a t e d o u t p u t f i l e 98 test_nucleus_in pu t ( hetest ) ; 99 100 / / t e s t " a d d i n g " t o g e t h e r t w o n u c l e i 101 Hydrogen. output ( cout ) << " + " ; 102 Helium. output ( cout ) << " -> " ; 103 add_nuclei ( Hydrogen , Helium ). output ( cout ) << endl ; 104 105 return 0 ; 106 }

Example Driver Console Output (1 neutron transferred)

Here is a nucleus: H 1 0

And here is another: He 2 2

Set Hydrogen with 2 neutrons: H 1 2

Neutrons to transfer from He 2 2 to H 1 2:

New Hydrogen: H 1 3

New Helium: He 2 1

First nucleus from data file H 1 3

Second nucleus from data file He 2 1

H 1 3 + He 2 1 -> Li 3 4

HeliumTest.txt Output File

H 1 3

He 2 1