CSE 292 Java Programming Professor Tong Lai Yu Homework 5 Santiago Campero 1) Follow the tutorial of the link, http://developer.android.com/resources/tutorials/views/hello-gridview.html to create the Grid View of the HelloWorld example in Android. Replace some of the icons provided by the site by your own icons. Choose Android version 2.3.3. Program Run
16
Embed
CSE 292 Java Programming Professor Tong Lai Yu Homework 5 ...cse.csusb.edu/tongyu/courses/cs292/homework/soln5/Campero2.pdf · private EditText tip20EditText; // displays 20% tip
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
CSE 292 Java ProgrammingProfessor Tong Lai Yu
Homework 5Santiago Campero
1) Follow the tutorial of the link,
http://developer.android.com/resources/tutorials/views/hello-gridview.html to create the Grid View of the HelloWorld example in Android. Replace some of the icons provided by the site by your own icons. Choose Android version 2.3.3.
//Lab Exercise 2 Solution: LinesJPanel.java//This program draws lines of different colorsimport java.awt.Color;import java.awt.BasicStroke;import java.awt.geom.Line2D;import java.awt.Graphics;import java.awt.Graphics2D;import java.util.Random;import javax.swing.JPanel;
public class LinesJPanel extends JPanel {private Color colors[] = { Color.GREEN, Color.CYAN, Color.YELLOW, Color.DARK_GRAY, Color.RED, Color.ORANGE, Color.GRAY, Color.PINK, Color.MAGENTA };// create 10 linespublic void paintComponent( Graphics g ){ super.paintComponent( g ); setBackground( Color.BLACK ); // set JPanel background Random random = new Random(); // get random number generator int thickness = 1; // default thickness int colorChoice = 0; // default color green int lineLenght = 1; //default lenght // create 2D by casting g to Graphics 2D Graphics2D g2d = ( Graphics2D ) g; for ( int y = 60; y < 250; y += 20 ) { // choose a random color from array /* Randomly choose an element of array colors and pass it to Graphics2D method setColor to specify the drawing color */ //pickrandom integerfrom0to8 colorChoice = random.nextInt(8 ); g2d.setPaint(colors[colorChoice]); // choose a random thickness from 1-20 /* Use Graphics2D method setStroke to randomly set the thickness of a line */ //pickrandom integerfrom1to20 thickness = 1+random.nextInt(20 ); g2d.setStroke(new BasicStroke(thickness) ); // choose a random length and draw line /* Create a random length line and use Graphics2D method draw to display the lines*/ lineLenght = 1+random.nextInt(300 ); g2d.draw(new Line2D.Double(0, y, lineLenght, y ) ); } // end for} // end method paintComponent} // end class LinesJPanel
4) Textbook, P.763, #17.8 (Telephone Number Word Generation) Standard telephone keypads contain the digits zero through nine. The numbers two through nine each have three letters associated with them as shown below:
Digit Letter Digit Letter Digit Letter
2 A B C 5 J K L 8 T U V
3 D E F 6 M N O 9 W X Y
4 G H I 7 P R S
For ease of memorization, people develop seven-letter words that correspond to their phone numbers. For example, one might use NUMBERS to represent 686-2377.Every seven-letter phone number corresponds to many different seven-letter words, many of them unrecognizable. Write a program that, given a seven-digit number, uses a PrintStream object to write to a file every possible seven-letter word combination corresponding to that number. There are 2,187 (37 such combinations. Avoid phone numbers with digits 0 and 1.
Program Run
PhoneNames.Java
package edu.CSE292.HW5d;
//public class PhoneNames {
import java.util.LinkedList;import java.util.List;public class PhoneNames { // Number-to-letter mappings in order from zero to nine public static String mappings[][] = { {"0"}, {"1"}, {"A", "B", "C"}, {"D", "E", "F"}, {"G", "H", "I"}, {"J", "K", "L"}, {"M", "N", "O"}, {"P", "Q", "R", "S"}, {"T", "U", "V"}, {"W", "X", "Y", "Z"} }; public static void generateCombosHelper(List<String> combos, String prefix, String remaining) { // The current digit we are working with int digit = Integer.parseInt(remaining.substring(0, 1));
if (remaining.length() == 1) { // We have reached the last digit in the phone number, so add // all possible prefix-digit combinations to the list for (int i = 0; i < mappings[digit].length; i++) { combos.add(prefix + mappings[digit][i]); } } else { // Recursively call this method with each possible new // prefix and the remaining part of the phone number. for (int i = 0; i < mappings[digit].length; i++) { generateCombosHelper(combos, prefix + mappings[digit][i], remaining.substring(1)); } } } public static List<String> generateCombos(String phoneNumber) { // This will hold the final list of combinations List<String> combos = new LinkedList<String>();
// Call the helper method with an empty prefix and the entire // phone number as the remaining part. generateCombosHelper(combos, "", phoneNumber); return combos; } public static void main(String[] args) { String phone = "3456789"; List<String> combos = generateCombos(phone); for (String s : combos) { System.out.println(s); } }}