SlideShare a Scribd company logo
1 of 4
Download to read offline
MIT Africa Information Technology Initiative  1 
MIT AITI Mobile Application Development in Java 
Lab 02: Variables and Operators 
 
Complete these problems and show your solutions to the instructors. Be prepared to explain your code.  
Create a new project in Eclipse named “lab02”. 
Part I: Written Exercises (20 Minutes) 
1. What is  wrong with each of the following statements: 
 
a) boolean isGood = 1;
b) char firstLetter = p;
c) int 2way = 89;
d) String name = Ricky;
e) int student score = 89;
f) Double $class = 4.5;
g) int _parents = 20.5;
h) string name = "Greg";
 
2. Without doing any programming, write down what the following main method prints to the screen? 
(Hint: Keep track of the values of all the variables for each line) 
public class UsingOperators {
public static void main(String[] args) {
int x = 5;
int y = 3;
int z = x + x*y - y;
System.out.println("The value of z is " + z);
int w = (x + x)*y - y;
System.out.println("The value of w is " + w);
z = w + 3;
System.out.println("The value of z is now " + z);
z -= 2;
System.out.println("The value of z is " + z);
z++;
System.out.println("The value of z is " + z);
--z;
System.out.println("The value of z is " + z);
boolean a = true;
MIT Africa Information Technology Initiative  2 
boolean b = false;
boolean c = ((a && (!(x > y))) && (a || y >x ));
System.out.println("c is " + c);
}
}
Part II: Computer Exercises 
3. Create a new Java file called UsingOperators and copy the above code into it.  Compile and run. 
Does the output match what you computed? 
 
4. Study the following code: 
class AddOne {
public static void main(String[] args) {
int c;
c = 32767;
System.out.println(c);
c += 1;
System.out.println(c);
}
}
 
What happens if you change the type of c to short?  Is the output different than the original 
version?  If so, why? 
5. Create a new Java file called TempConverter.  Add the following code to the file: 
class TempConverter {
public static void main(String[] args) {
//place your code here
}
}
Add code that declares and initializes a variable to store the temperature in Celsius. Your 
temperature variable should be stored numbers with decimal places.  Which types would be 
appropriate to use for variables with decimal places? 
 
 
6. Compute the temperature in Fahrenheit according to the following formula and print it to the 
screen.  Note,   Fahrenheit = 9/5 * Celsius + 32 
 
7. Set the Celsius variable to 100 and compile and run TempConverter. The correct output is 212.0. 
If your output was 132, you probably used integer division somewhere by mistake. 
 
MIT Africa Information Technology Initiative  3 
8. Now we will create a program that will compute an average using two different variable types.  
First create a Java file called Fruit.  Type this code into the file.   
class Fruit{
public static void main(String[] args) {
// Declare and initialize three variables here
}
}
Initialize three variables: 
1. An integer representing the number of fruits a boy is carrying. 
2. An integer representing the number of fruits a girl is carrying. 
3. The average number of fruits between them.  
 
 
 
Declare the number of fruits the boy is carrying to be 5 and the number of fruits the girl is carrying 
to be 10.  
 
Compute and print the precise average number of fruits between both people.  
 
 Challenge Problems  
 
1. Write a program that sums the individual digits of an integer.  For example, if an integer is 932, the 
sum is 14.  Hint: Use the % operator to extract digits and use the / operator to remove the 
extracted digit.  For instance, 932 % 10 = 2 and 932 / 10 = 93. 
 
2.  Program the “Penny Ante” game. The game works as follows: two players each pick a sequence of 
heads and tails (for example heads‐tails‐tails or “htt”). The two sequences should be of the same 
length but cannot be equal. They then flip a coin and record the sequence of flips (another 
sequence of heads and tails). Whichever player’s sequence appears first in the sequence of tosses 
is the winner. For example, Player 1’s sequence might be “ht” (heads‐tails) and Player 2’s might be 
“hh”. The sequence of coin tosses might be “tthh” in which case Player 2 is the winner. They 
should keep flipping the coin until one player or the other wins. 
 
To program the Penny Ante, scan in two inputs which will be the heads/tails sequences of the two 
players. Flip a coin until one player wins and print the sequence of tosses as well as the winning 
player. 
 
       It is not necessary, but consider checking the sequence inputs to see if they are actually distinct    
       sequences of heads and tails of equal length. Keep prompting the user to enter new sequences.   
       See if you can start a new game after each one ends. 
 
   The skeleton code contains code for a random flip and to scan user input. 
 
MIT Africa Information Technology Initiative  4 
3. a)   Write a method that takes an array of integers and another integer as arguments. If the 
integer is the sum of two different elements of the array, return true. Otherwise, return 
false. 
 
For example, if your inputs were {1,3,6} and 9, the method would return true since 9 
= 6 + 3. If your inputs were {1,3,6} and 10, the method would return false. If your 
inputs were {1,3,6} and 6, your method would return false: even though 6 = 3 + 3, 
you want to use distinct elements of the array. 
 
You may assume that the array has no duplicated values (no arrays like {1,3,6,6}). 
b) Extend the previous problem so that your method has two int inputs. The second int   
input determines the number of (distinct) elements that should be added to make the first 
integer input. 
 
For example, if your inputs were {1,3,6,7}, 11, and 3, your method would return true 
as 11 = 1 + 6 + 7. If your inputs were {1,3,6,7}, 17, and 4, your method would return 
true as 17 = 1 + 3 + 6 + 7. 
4. Write a recursive program that extends the range of the Fibonacci sequence.  The Fibonacci 
sequence is 1 1 2 3 5 8 etc. where each element is the sum of the previous two elements. For 
this problem, instead of only adding the last two elements to get the nth element, have the nth 
element be the sum of the previous k elements.  For example, the 7th Fibonacci element 
summing the previous 3 elements would be 37.  The sequence is   1 1 2 4 7 13.  Be sure to check 
your code by running it with a k value of 2 and comparing the value to the value you would 
obtain with the regular Fibonacci sequence (they should be the same).  
5. Write a program that computes the factorial of a non‐negative integer. For example, the 
factorial of 4  or 4!  is 4 x 3 x 2 x1 = 24.  If you understand recursion, you may use that to write 
your program. 
6. Write a program that will test to see whether or not a given String is a palindrome.    A 
palindrome is a word or phrase which reads the same in both directions. examples: racecar; 
level; dad; mom; madam;  If a word is a palindrome, print out a statement saying it is. If not, 
then print out a statement saying it is not. 
 
 
 
 
 

More Related Content

Viewers also liked

Software Architecture - Quiz Questions
Software Architecture - Quiz QuestionsSoftware Architecture - Quiz Questions
Software Architecture - Quiz QuestionsGanesh Samarthyam
 
Final Exam - Practice Questions
Final Exam - Practice QuestionsFinal Exam - Practice Questions
Final Exam - Practice QuestionsDaniel Overbey
 
Grade 9 social studies test on groups
Grade 9 social studies test on groupsGrade 9 social studies test on groups
Grade 9 social studies test on groupsDeighton Gooden
 
Classroom Management
Classroom Management Classroom Management
Classroom Management DrAbey Thomas
 
Kounin Model
Kounin ModelKounin Model
Kounin Modelkal2129
 
Computer graphics mcq question bank
Computer graphics mcq question bankComputer graphics mcq question bank
Computer graphics mcq question banksuthi
 
Classroom management pioneers
Classroom management pioneersClassroom management pioneers
Classroom management pioneerslelliott22326
 
Methods theoriesof management
Methods theoriesof managementMethods theoriesof management
Methods theoriesof managementWolmerian
 
Teaching profession: Why have I chosen teaching as profession
Teaching profession: Why have I chosen teaching as professionTeaching profession: Why have I chosen teaching as profession
Teaching profession: Why have I chosen teaching as professionHina Honey
 
Educational media and technology solved multiple choice questions
Educational media and technology solved multiple choice questionsEducational media and technology solved multiple choice questions
Educational media and technology solved multiple choice questionsedsonmnyambo
 
MULTIPLE CHOICE QUESTIONS WITH ANSWERS ON NETWORK MANAGEMENT SYSTEMS
MULTIPLE CHOICE QUESTIONS WITH ANSWERS ON NETWORK MANAGEMENT SYSTEMSMULTIPLE CHOICE QUESTIONS WITH ANSWERS ON NETWORK MANAGEMENT SYSTEMS
MULTIPLE CHOICE QUESTIONS WITH ANSWERS ON NETWORK MANAGEMENT SYSTEMSvtunotesbysree
 
Classroom management theory presentation.notes.pptx.pdf
Classroom management theory presentation.notes.pptx.pdfClassroom management theory presentation.notes.pptx.pdf
Classroom management theory presentation.notes.pptx.pdfIan Glasmann
 
Teachingprofession
TeachingprofessionTeachingprofession
TeachingprofessionMykel Tuazon
 
LET review in Social Science
LET review in Social ScienceLET review in Social Science
LET review in Social ScienceRaiza Joy Orcena
 
Social Studies Notes And Revision
Social Studies Notes And RevisionSocial Studies Notes And Revision
Social Studies Notes And RevisionDeighton Gooden
 
Educational Technology Exam Review
Educational Technology Exam ReviewEducational Technology Exam Review
Educational Technology Exam Reviewjwheetley
 

Viewers also liked (19)

Software Architecture - Quiz Questions
Software Architecture - Quiz QuestionsSoftware Architecture - Quiz Questions
Software Architecture - Quiz Questions
 
Computer hero
Computer heroComputer hero
Computer hero
 
Social 2 reviewer
Social 2 reviewerSocial 2 reviewer
Social 2 reviewer
 
Final Exam - Practice Questions
Final Exam - Practice QuestionsFinal Exam - Practice Questions
Final Exam - Practice Questions
 
Grade 9 social studies test on groups
Grade 9 social studies test on groupsGrade 9 social studies test on groups
Grade 9 social studies test on groups
 
Classroom Management
Classroom Management Classroom Management
Classroom Management
 
Kounin Model
Kounin ModelKounin Model
Kounin Model
 
Computer graphics mcq question bank
Computer graphics mcq question bankComputer graphics mcq question bank
Computer graphics mcq question bank
 
Classroom management pioneers
Classroom management pioneersClassroom management pioneers
Classroom management pioneers
 
Methods theoriesof management
Methods theoriesof managementMethods theoriesof management
Methods theoriesof management
 
Teaching profession: Why have I chosen teaching as profession
Teaching profession: Why have I chosen teaching as professionTeaching profession: Why have I chosen teaching as profession
Teaching profession: Why have I chosen teaching as profession
 
Educational media and technology solved multiple choice questions
Educational media and technology solved multiple choice questionsEducational media and technology solved multiple choice questions
Educational media and technology solved multiple choice questions
 
Mcq for final
Mcq for finalMcq for final
Mcq for final
 
MULTIPLE CHOICE QUESTIONS WITH ANSWERS ON NETWORK MANAGEMENT SYSTEMS
MULTIPLE CHOICE QUESTIONS WITH ANSWERS ON NETWORK MANAGEMENT SYSTEMSMULTIPLE CHOICE QUESTIONS WITH ANSWERS ON NETWORK MANAGEMENT SYSTEMS
MULTIPLE CHOICE QUESTIONS WITH ANSWERS ON NETWORK MANAGEMENT SYSTEMS
 
Classroom management theory presentation.notes.pptx.pdf
Classroom management theory presentation.notes.pptx.pdfClassroom management theory presentation.notes.pptx.pdf
Classroom management theory presentation.notes.pptx.pdf
 
Teachingprofession
TeachingprofessionTeachingprofession
Teachingprofession
 
LET review in Social Science
LET review in Social ScienceLET review in Social Science
LET review in Social Science
 
Social Studies Notes And Revision
Social Studies Notes And RevisionSocial Studies Notes And Revision
Social Studies Notes And Revision
 
Educational Technology Exam Review
Educational Technology Exam ReviewEducational Technology Exam Review
Educational Technology Exam Review
 

More from Daroko blog(www.professionalbloggertricks.com)

More from Daroko blog(www.professionalbloggertricks.com) (20)

Small Business ideas you can start in Nigeria 2014(best Business ideas Nigeri...
Small Business ideas you can start in Nigeria 2014(best Business ideas Nigeri...Small Business ideas you can start in Nigeria 2014(best Business ideas Nigeri...
Small Business ideas you can start in Nigeria 2014(best Business ideas Nigeri...
 
Agriculture business ideas for 2014(Business ideas Kenya,Business ideas Niger...
Agriculture business ideas for 2014(Business ideas Kenya,Business ideas Niger...Agriculture business ideas for 2014(Business ideas Kenya,Business ideas Niger...
Agriculture business ideas for 2014(Business ideas Kenya,Business ideas Niger...
 
An Introduction to Project management(project management tutorials)
An Introduction to Project management(project management tutorials)An Introduction to Project management(project management tutorials)
An Introduction to Project management(project management tutorials)
 
java arlow jdbc tutorial(java programming tutorials)
java arlow jdbc tutorial(java programming tutorials)java arlow jdbc tutorial(java programming tutorials)
java arlow jdbc tutorial(java programming tutorials)
 
The java rogramming swing _tutorial for beinners(java programming language)
The java rogramming swing _tutorial for beinners(java programming language)The java rogramming swing _tutorial for beinners(java programming language)
The java rogramming swing _tutorial for beinners(java programming language)
 
Java programming basics notes for beginners(java programming tutorials)
Java programming basics notes for beginners(java programming tutorials)Java programming basics notes for beginners(java programming tutorials)
Java programming basics notes for beginners(java programming tutorials)
 
advanced java programming(java programming tutorials)
 advanced java programming(java programming tutorials) advanced java programming(java programming tutorials)
advanced java programming(java programming tutorials)
 
java swing tutorial for beginners(java programming tutorials)
java swing tutorial for beginners(java programming tutorials)java swing tutorial for beginners(java programming tutorials)
java swing tutorial for beginners(java programming tutorials)
 
An introduction to java programming language forbeginners(java programming tu...
An introduction to java programming language forbeginners(java programming tu...An introduction to java programming language forbeginners(java programming tu...
An introduction to java programming language forbeginners(java programming tu...
 
fundamentals of Computer graphics(Computer graphics tutorials)
 fundamentals of Computer graphics(Computer graphics tutorials) fundamentals of Computer graphics(Computer graphics tutorials)
fundamentals of Computer graphics(Computer graphics tutorials)
 
bresenham circles and polygons in computer graphics(Computer graphics tutorials)
bresenham circles and polygons in computer graphics(Computer graphics tutorials)bresenham circles and polygons in computer graphics(Computer graphics tutorials)
bresenham circles and polygons in computer graphics(Computer graphics tutorials)
 
Computer Graphics display technologies(Computer graphics tutorials and tips)
Computer Graphics display technologies(Computer graphics tutorials and tips)Computer Graphics display technologies(Computer graphics tutorials and tips)
Computer Graphics display technologies(Computer graphics tutorials and tips)
 
Computer Graphics display technologies(Computer graphics tutorials)
Computer Graphics display technologies(Computer graphics tutorials)Computer Graphics display technologies(Computer graphics tutorials)
Computer Graphics display technologies(Computer graphics tutorials)
 
Displays and color system in computer graphics(Computer graphics tutorials)
Displays and color system in computer graphics(Computer graphics tutorials)Displays and color system in computer graphics(Computer graphics tutorials)
Displays and color system in computer graphics(Computer graphics tutorials)
 
Data structures graphics library in computer graphics.
Data structures  graphics library in computer graphics.Data structures  graphics library in computer graphics.
Data structures graphics library in computer graphics.
 
lecture8 clipping
lecture8 clippinglecture8 clipping
lecture8 clipping
 
Csc406 lecture7 device independence and normalization in Computer graphics(Co...
Csc406 lecture7 device independence and normalization in Computer graphics(Co...Csc406 lecture7 device independence and normalization in Computer graphics(Co...
Csc406 lecture7 device independence and normalization in Computer graphics(Co...
 
lecture4 raster details in computer graphics(Computer graphics tutorials)
lecture4 raster details in computer graphics(Computer graphics tutorials)lecture4 raster details in computer graphics(Computer graphics tutorials)
lecture4 raster details in computer graphics(Computer graphics tutorials)
 
lecture3 color representation in computer graphics(Computer graphics tutorials)
lecture3 color representation in computer graphics(Computer graphics tutorials)lecture3 color representation in computer graphics(Computer graphics tutorials)
lecture3 color representation in computer graphics(Computer graphics tutorials)
 
lecture2 computer graphics graphics hardware(Computer graphics tutorials)
 lecture2  computer graphics graphics hardware(Computer graphics tutorials) lecture2  computer graphics graphics hardware(Computer graphics tutorials)
lecture2 computer graphics graphics hardware(Computer graphics tutorials)
 

Recently uploaded

Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersChitralekhaTherkar
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 

Recently uploaded (20)

Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of Powders
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 

An Introduction to java programming(fundamentals of java programming)

  • 1. MIT Africa Information Technology Initiative  1  MIT AITI Mobile Application Development in Java  Lab 02: Variables and Operators    Complete these problems and show your solutions to the instructors. Be prepared to explain your code.   Create a new project in Eclipse named “lab02”.  Part I: Written Exercises (20 Minutes)  1. What is  wrong with each of the following statements:    a) boolean isGood = 1; b) char firstLetter = p; c) int 2way = 89; d) String name = Ricky; e) int student score = 89; f) Double $class = 4.5; g) int _parents = 20.5; h) string name = "Greg";   2. Without doing any programming, write down what the following main method prints to the screen?  (Hint: Keep track of the values of all the variables for each line)  public class UsingOperators { public static void main(String[] args) { int x = 5; int y = 3; int z = x + x*y - y; System.out.println("The value of z is " + z); int w = (x + x)*y - y; System.out.println("The value of w is " + w); z = w + 3; System.out.println("The value of z is now " + z); z -= 2; System.out.println("The value of z is " + z); z++; System.out.println("The value of z is " + z); --z; System.out.println("The value of z is " + z); boolean a = true;
  • 2. MIT Africa Information Technology Initiative  2  boolean b = false; boolean c = ((a && (!(x > y))) && (a || y >x )); System.out.println("c is " + c); } } Part II: Computer Exercises  3. Create a new Java file called UsingOperators and copy the above code into it.  Compile and run.  Does the output match what you computed?    4. Study the following code:  class AddOne { public static void main(String[] args) { int c; c = 32767; System.out.println(c); c += 1; System.out.println(c); } }   What happens if you change the type of c to short?  Is the output different than the original  version?  If so, why?  5. Create a new Java file called TempConverter.  Add the following code to the file:  class TempConverter { public static void main(String[] args) { //place your code here } } Add code that declares and initializes a variable to store the temperature in Celsius. Your  temperature variable should be stored numbers with decimal places.  Which types would be  appropriate to use for variables with decimal places?      6. Compute the temperature in Fahrenheit according to the following formula and print it to the  screen.  Note,   Fahrenheit = 9/5 * Celsius + 32    7. Set the Celsius variable to 100 and compile and run TempConverter. The correct output is 212.0.  If your output was 132, you probably used integer division somewhere by mistake.   
  • 3. MIT Africa Information Technology Initiative  3  8. Now we will create a program that will compute an average using two different variable types.   First create a Java file called Fruit.  Type this code into the file.    class Fruit{ public static void main(String[] args) { // Declare and initialize three variables here } } Initialize three variables:  1. An integer representing the number of fruits a boy is carrying.  2. An integer representing the number of fruits a girl is carrying.  3. The average number of fruits between them.         Declare the number of fruits the boy is carrying to be 5 and the number of fruits the girl is carrying  to be 10.     Compute and print the precise average number of fruits between both people.      Challenge Problems     1. Write a program that sums the individual digits of an integer.  For example, if an integer is 932, the  sum is 14.  Hint: Use the % operator to extract digits and use the / operator to remove the  extracted digit.  For instance, 932 % 10 = 2 and 932 / 10 = 93.    2.  Program the “Penny Ante” game. The game works as follows: two players each pick a sequence of  heads and tails (for example heads‐tails‐tails or “htt”). The two sequences should be of the same  length but cannot be equal. They then flip a coin and record the sequence of flips (another  sequence of heads and tails). Whichever player’s sequence appears first in the sequence of tosses  is the winner. For example, Player 1’s sequence might be “ht” (heads‐tails) and Player 2’s might be  “hh”. The sequence of coin tosses might be “tthh” in which case Player 2 is the winner. They  should keep flipping the coin until one player or the other wins.    To program the Penny Ante, scan in two inputs which will be the heads/tails sequences of the two  players. Flip a coin until one player wins and print the sequence of tosses as well as the winning  player.           It is not necessary, but consider checking the sequence inputs to see if they are actually distinct            sequences of heads and tails of equal length. Keep prompting the user to enter new sequences.           See if you can start a new game after each one ends.       The skeleton code contains code for a random flip and to scan user input.   
  • 4. MIT Africa Information Technology Initiative  4  3. a)   Write a method that takes an array of integers and another integer as arguments. If the  integer is the sum of two different elements of the array, return true. Otherwise, return  false.    For example, if your inputs were {1,3,6} and 9, the method would return true since 9  = 6 + 3. If your inputs were {1,3,6} and 10, the method would return false. If your  inputs were {1,3,6} and 6, your method would return false: even though 6 = 3 + 3,  you want to use distinct elements of the array.    You may assume that the array has no duplicated values (no arrays like {1,3,6,6}).  b) Extend the previous problem so that your method has two int inputs. The second int    input determines the number of (distinct) elements that should be added to make the first  integer input.    For example, if your inputs were {1,3,6,7}, 11, and 3, your method would return true  as 11 = 1 + 6 + 7. If your inputs were {1,3,6,7}, 17, and 4, your method would return  true as 17 = 1 + 3 + 6 + 7.  4. Write a recursive program that extends the range of the Fibonacci sequence.  The Fibonacci  sequence is 1 1 2 3 5 8 etc. where each element is the sum of the previous two elements. For  this problem, instead of only adding the last two elements to get the nth element, have the nth  element be the sum of the previous k elements.  For example, the 7th Fibonacci element  summing the previous 3 elements would be 37.  The sequence is   1 1 2 4 7 13.  Be sure to check  your code by running it with a k value of 2 and comparing the value to the value you would  obtain with the regular Fibonacci sequence (they should be the same).   5. Write a program that computes the factorial of a non‐negative integer. For example, the  factorial of 4  or 4!  is 4 x 3 x 2 x1 = 24.  If you understand recursion, you may use that to write  your program.  6. Write a program that will test to see whether or not a given String is a palindrome.    A  palindrome is a word or phrase which reads the same in both directions. examples: racecar;  level; dad; mom; madam;  If a word is a palindrome, print out a statement saying it is. If not,  then print out a statement saying it is not.