The Mag class, given below, would be useful for a small business that sells year
ID: 665180 • Letter: T
Question
The Mag class, given below, would be useful for a small business that sells yearly magazine subscriptions to the public. The attributes for this class are: a magazine's name; the number of issues in a yearly subscription (e.g., 6, 8, or 12); and the price, or cost, of a yearly subscription.
public class Mag{
private String name; // magazine name
private int issues; // issues in yearly subscription
private double cost; // cost of yearly subscription
public Mag(String n, int i, double cost){
name = n;
issues = i;
this.cost = cost;
}
public String getName(){return name;}
public int getIssues(){return issues;}
public double getCost(){return cost;}
}
Suppose mags is an array of Mag objects that appears in a driver class, say MagDriver. Write a code fragment in this driver class that uses a for-each loop to print the number of objects in array mags that EITHER cost less than $40 per year OR have fewer than 8 issues in a year.
Explanation / Answer
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Mag{
private String name; // magazine name
private int issues; // issues in yearly subscription
private double cost; // cost of yearly subscription
public Mag(String n, int i, double cost){
name = n;
issues = i;
this.cost = cost;
}
public String getName()
{
return name;
}
public int getIssues()
{
return issues;
}
public double getCost(){return cost;}
}
class MagDriver
{
public static void main(String args[])
{
List<Mag> mags = new ArrayList();
m1.Mag("ABC", 6, 44.00);
mags.add(m1);
m2.Mag("ABCD", 7, 50.00);
mags.add(m2);
m3.Mag("ABC", 8, 100.00);
mags.add(m3);
System.out.println("Add new Magazines: ");
System.out.println("Enter number of Magazines to add: ");
int countMagazines = input.nextInt();
for(int i=0;i<countMagazines;i++)
{
if(mags[i].getCost<40) || mags[i].getissues<8)
{
System.out.println(mags[i]);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.