0% found this document useful (0 votes)
28 views3 pages

Sheet 9

Uploaded by

Mohamed Omar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views3 pages

Sheet 9

Uploaded by

Mohamed Omar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

Misr University for Science and Technology Course Code: CS 102

Faculty of Information Technology Title: Structured Programming


Coordinator: Prof. Arabi Keshk

Sheet 9

1. Write a Java class called Holiday as stated below. An object of class Holiday represents a
holiday during the year. This class has three instance variables:
 name, which is a String representing the name of the holiday
 day, which is an int representing the day of the month of the holiday
 month, which is a String representing the month the holiday is in.
a. Write a constructor for the class Holiday, which takes a String representing the name,
an int representing the day, and a String representing the month as its arguments, and
sets the class variables to these values.
b. Write a method inSameMonth, which compares two instances of the class Holiday,
and returns the Boolean value true if they have the same month, and false if they do
not.
c. Write a method avgDate which takes an array of base type Holiday as its argument,
and returns a double that is the average of the day variables in the Holiday instances
in the array. You may assume that the array is full (i.e. does not have any null
entries).
d. Write a piece of code that creates a Holiday instance with the name “Independence
Sina Day”, with the day “25”, and with the month “April”.
public class Holiday {
String name;
int day;
String month;
public Holiday(String n,int d,String m)
{
name = n;
day = d;
month = m;
}
public boolean inSameMonth(Holiday h)
{
if(h.month.equals(month))
return true;
return false;
}
public static double avgDate (Holiday[] a)
{
double total = 0;
for(int i=0;i<a.length;i++)
{
total = total + a[i].day;
}
return total;
}

public static void main(String[] args)


{
Holiday sina = new Holiday("Sina", 25, "April");
Holiday shamNesem = new Holiday("Sham", 9, "April");
System.out.println(sina.inSameMonth(shamNesem));
}
}
2. Write the Java class Movie as stated below. An instance of class Movie represents a film.
This class has the following three class variables:
 title, which is a String representing the title of the movie
 studio, which is a String representing the studio that made the movie
 rating, which is a String representing the rating of the movie (i.e. PG13, R, etc)
a. Write a constructor for the class Movie, which takes a String representing the title of
the movie, a String representing the studio, and a String representing the rating as its
arguments, and sets the respective class variables to these values.
b. Write a second constructor for the class Movie, which takes a String representing the
title of the movie and a String representing the studio as its arguments, and sets the
respective class variables to these values, while the class variable rating is set to
"PG".
c. Write a method getPG, which takes an array of base type Movie as its argument, and
returns a new array of only those movies in the input array with a rating of "PG". You
may assume the input array is full of Movie instances. The returned array need not be
full.
d. Write a piece of code that creates an instance of the class Movie with the title “Casino
Royale”, the studio “Eon Productions”, and the rating “PG13”.
public class Movie {
String title,studio,rating;
public Movie(String t,String s,String r)
{
title = t;
studio = s;
rating = r;
}
public Movie(String t,String s)
{
this(t,s,"PG");
}
public static Movie[] getPG(Movie[] m)
{
int pgCount =0;
for(int i=0;i<m.length;i++)
if(m[i].rating.equalsIgnoreCase("pg"))
pgCount++;
Movie[] pgMovies = new Movie[pgCount];
int pgIndex = 0;
for(int i=0;i<m.length;i++)
if(m[i].rating.equalsIgnoreCase("pg"))
pgMovies[pgIndex++] = m[i];
return pgMovies;
}
public static void main(String[] args)
{

Movie[] movies = new Movie[3];


movies[0] = new Movie("m1", "s1");;
movies[1] = new Movie("m2", "s2", "2");;
movies[2] = new Movie("m3", "s3");
Movie[] temp = Movie.getPG(movies);
for(int i=0;i<temp.length;i++)
System.out.println(temp[i].title+" "+temp[i].studio+"
"+temp[i].rating);

}
}

You might also like