Help with writing program. Look at Programming Exercise 10 on page 381 for the p
ID: 3697242 • Letter: H
Question
Help with writing program. Look at Programming Exercise 10 on page 381 for the program description. 1. Create the sieve function. # initially, all values are in the sieve of candidates candidates = list(range(2,n+1)) primes = [] while candidates != []: # remove first item, it's prime newPrime = candidates[0] primes.append(newPrime) # create new candidate list of all the non-divisible candidates cands2 = [] for c in candidates: if c % newPrime != 0: cands2.append(c) candidates = cands2 return primes 2. Create the main function. def main(): print("Sieve of Eratosthenes ") n = eval(input("Enter upper limit: ")) primes = sieve(n) for p in primes: print(p) 3. Call main Create a class Deck that represents a deck of cards. Your class should have the following methods: constructor: Creates a new deck of 52 cards in a standard order. shuffle: Randomizes the order of the cards. dealCard: Returns a single card from the top of the deck and removes the card from the deck. cardsLeft: Returns the number of cards remaining in the deck. Test your program by having it deal out a sequence of n cards from a shuffled deck where n is a user input. 1. Create a class deck class Deck: a. Create the constructor def __init__(self): self.cards = [] for suit in "cdhs": for rank in range(1,14): self.cards.append(Card(rank,suit)) b. Create the shuffle method def shuffle(self): n = len(self.cards) cards = self.cards for pos in range(n): randpos = randrange(pos,n) cards[pos], cards[randpos] = cards[randpos], cards[pos] //Note this belongs on the same line as above. c. Create the dealCard method def dealCard(self): return self.cards.pop() d. Create the cardsLeft method def cardsLeft(self): return len(self.cards) //As part of the constructor for the deck, the code requires that //an instance of a card be created each iteration of the loop (See //line 5 of 4a). We now have to create the class that will //generate that card. 2. Create a card class class Card: a. Create a constructor def __init__(self, rank, suit): self.rank = rank self.suit = suit b. Create accessors def getRank(self): return self.rank def getSuit(self): return self.suit c. Create a method that determines value of a card with everything below 10 will be face value. Everything 10 or above will have a value of 10. def BJValue(self): if self.getRank() < 10: return self.rank else: return 10 d. Create a method that converts rank and suit to a string value. def __str__(self): ranks = [None, "Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"] rankStr = ranks[self.rank] if self.suit == 'c': suitStr = "Clubs" elif self.suit == 'd': suitStr = "Diamonds" elif self.suit == 'h': suitStr = "Hearts" else: suitStr = "Spades" return "{0} of {1}".format(rankStr, suitStr) 3. Create the main function definition. def main(): n = eval(input("Hose many cards should I deal? ")) d = Deck() d.shuffle() for i in range(n): print(d.dealCard()) 4. Call main.
Explanation / Answer
import javax.swing.*; public class SwingLayoutDemo { private JFrame mainFrame; private JLabel headerLabel; private JLabel statusLabel; private JPanel controlPanel; private JLabel msglabel; public SwingLayoutDemo(){ prepareGUI(); } public static void main(String[] args){ SwingLayoutDemo swingLayoutDemo = new SwingLayoutDemo(); swingLayoutDemo.showGridLayoutDemo(); } private void prepareGUI(){ mainFrame = new JFrame("Java SWING Examples"); mainFrame.setSize(400,400); mainFrame.setLayout(new GridLayout(3, 1)); headerLabel = new JLabel("",JLabel.CENTER ); statusLabel = new JLabel("",JLabel.CENTER); statusLabel.setSize(350,100); mainFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent windowEvent){ System.exit(0); } }); controlPanel = new JPanel(); controlPanel.setLayout(new FlowLayout()); mainFrame.add(headerLabel); mainFrame.add(controlPanel); mainFrame.add(statusLabel); mainFrame.setVisible(true); } private void showGridLayoutDemo(){ headerLabel.setText("Layout in action: GridLayout"); JPanel panel = new JPanel(); panel.setBackground(Color.darkGray); panel.setSize(300,300); GridLayout layout = new GridLayout(0,3); layout.setHgap(10); layout.setVgap(10); panel.setLayout(layout); panel.add(new JButton("Button 1")); panel.add(new JButton("Button 2")); panel.add(new JButton("Button 3")); panel.add(new JButton("Button 4")); panel.add(new JButton("Button 5")); controlPanel.add(panel); mainFrame.setVisible(true); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.