STEV
STEV
Create a class Rectangle with attributes length and width, each of which defaults to 2.
Provide Methods that calculate the rectangle’s perimeter and area. It has set and get methods
for both Length and width. The set methods should verify that length and width are each
floating-point Numbers larger than 10.0 and less than 40.0. Write a program to test class
Rectangle.
Answer:
class RectangleTest{
float area;
float perimeter;
//set and get methods
void setPerim(float length, float width){
perimeter= (length+width)*2;
}
void setArea(float length, float width){
area=width*length;
}
void getPerim(){
System.out.printf("\nTatal perimeter is %f", perimeter);
}
void getArea(){
System.out.printf("\nTotal area is %f", area);
}
}
Task2.
Consider a 3-by-4 integer array k.
a) Write a statement that declares and creates k .
b) How many rows and column does k have?
c) How many elements does k have?
d) Write access expressions for all the elements in row 3 of k .
e) Write a single statement that sets the element of k in row 0 and column 1 to zero.
f) Write individual statements to initialize each element of k to zero.
g) Write a nested for statement that inputs the values for the elements of k from the user.
h) Write a series of statements that determines and displays the smallest value in k .
i) Write a statement that totals the elements of the third column of k . Do not use repetition.
j) Write a series of statements that displays the contents of k in tabular format. List the
column indices as headings across the top, and list the row indices at the left of each row.
Answer:
a) Intt[] = new int[3][4];
b) k has 3 rows
c) 3 * 4 = 12
k has 12 elements
d) k[3][0], k[3][1], k[3][2], k[3][3]
e) k[0][1] = 0
f) k[0][0] = 0;
k[0][1] = 0;
k[0][2] = 0;
k[0][3] = 0;
k[1][0] = 0;
k[1][1] = 0;
k[1][2] = 0;
k[1][3] = 0;
g) for( inti=0; i<k.length; i++)
for(int j=0; j<k[i].length; j++)
k[i][j] = inut.nextInt();
h) int smallest = k[0][0];
for( inti=0; i<k.length; i++)
for(int j=0; j<k[i].length; j++)
if(k[x][y] < smallest)
smallest = k[x][y];
System.out.println(Smallest);
i) int total =k[0][2] + k[1][2];
j) System.out.println(“\k0\k1\k2\n”);
For(int e =0; e<k.length; e++)
{
System.out.print(e);
for( int r = 0; r<k[e].length; r++)
System.out.println(“\k%d”,k[e][r]);
System.out.println();
} // End for
import java.util.*;
public class Qn2 {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
//int [][] k =new int [3][4];
//Declaretion and Innitialization of k;
int [][] k= {{1,2,3,4},{5,6,7,8},{9,6,3,2}};
int i,j, count=0;
for(i=0; i<3;i++){
for(j=0;j<4;j++){
//count++;
System.out.printf(" "+k[i][j]);
}
System.out.printf("\n");
}
//Counting element of k
count=i*j;
System.out.println("There are element: "+count);
//nested for statement that inputs the values for the elements of k from the user.
System.out.println("enter the element of the matrix");
for(i=0;i<3;i++){
for(j=0;j<4;j++){
k [i][j] =input.nextInt();
}
}
for(i=0; i<3;i++){
for(j=0;j<4;j++){
System.out.printf(" "+k[i][j]);
}
System.out.printf("\n");
}
Task 3
Write a Java program that computes a customer’s monthly cell phone bill given the number
of minutes used. The program should take in two integer command-line arguments—one for
the number of peak minutes used, and one for the number of weekend and night minutes
used. Each customer pays TSh.5000/= a month, which includes 400 peak minutes and 750
weekend and night minutes. The price for going over the allotted time is TSh.150/= per
minute for both peak and weekend/night calls.
import java.util.Scanner;
public class Qn3{
public static void main(String[]args){
int charge=10000;
int peak,charge1=0,charge2=0,charge3=0;
int weekendAndNight;
Scanner input=new Scanner(System.in);
System.out.println("enter the peak minutes");
peak=input.nextInt();
System.out.println("enter the weekend and night minutes");
weekendAndNight=input.nextInt();
if(peak==400 && weekendAndNight==750){
System.out.printf("The amount is",charge);
}
else if(peak>400 || weekendAndNight>750){
charge1=(peak-400)+(weekendAndNight-750);
System.out.printf("\nExtra amount is",charge1);
}
else if(peak<400 || weekendAndNight<750){
charge2=400-peak;
charge3=750-weekendAndNight;
System.out.printf("%d peak minutes left and %d weekendAndNight minutes
left",charge2,charge3);
}
}
}
TASK 4
Define an abstract base class Shape that includes protected data members for the (x, y)
position of a shape,
a public method to move a shape, and a public abstract method show() to output a shape.
Derive subclasses
for lines, circles, and rectangles. Also, define the class PolyLine that you saw in this chapter
with Shape as
its base class. You can represent a line as two points, a circle as a center and a radius, and a
rectangle as
two points on diagonally opposite corners. Implement the toString() method for each class.
Test the classes
by selecting ten random objects of the derived classes, and then invoking the show() method
for each. Use
the toString() methods in the derived classes
SOLUTION 4
import java.util.Scanner;
import shapi.*;
int ch=0;
double dx=0;
double dy=0;
while(true)
System.out.println("1.line");
System.out.println("2.circle");
System.out.println("3.Rectangle");
System.out.println("4.polyline");
System.out.println("5.Exit");
ch=sc.nextInt();
if(ch>5)
{
System.out.println("Enter(dx,dy):");
dx=sc.nextDouble();
dy=sc.nextdouble();
switch(ch){
case 1:
System.out.println(L1);
L1.move(dx,dy);
System.out.println("move coordinates");
System.out.println(L1);
break;
case 2:
System.out.println(c1);
L1.move(dx,dy);
System.out.println("move coordinates");
System.out.println(c1);
break;
case 3:
System.out.println(r1);
L1.move(dx,dy);
System.out.println("move coordinates");
System.out.println(r1);
break;
case 4:
System.out.println(pl1);
L1.move(dx,dy);
System.out.println("move coordinates");
System.out.println(pl1);
L1.move(dx,dy);
System.out.println("move coordinates");
System.out.println(r1);
break;
case 5:
System.out.println("program is exiting");
System.exit(0);
import shapi.Shape;
this.x1=x1;
this.y1=y1;
this.x2=x2;
this.y2=y2;
super.move(dx,dy);
this.x2=this.x2+dx;
this.y2=y2+dy;
return show();
@ Override
return null;
//Circle
import shapi.Shape;
private double r;
this.x1=x1;
this.y1=y1;
this.r=r;
super.move(dx,dy);
return show();
@ Override
return null;
// Rectangle
import shapi.Shape;
public class Rectangles extends Shape{
this.x1=x1;
this.y1=y1;
this.x2=x2;
this.y2=y2;
super.move(dx,dy);
this.x2=this.x2+dx;
this.y2=this.y2+dy;
return show();
@ Override
return null;
//polylines
import shapi.Shape;
this.x1=x1;
this.y1=y1;
this.x2=x2;
this.y2=y2;
this.x3=x3;
this.y3=y3;
super.move(dx,dy);
this.x2=this.x2+dx;
this.y2=this.y2+dy;
this.x3=this.x3+dx;
this.y3=this.y3+dy;
return Show();
@ override
return null;
}
Task 5
Create the following GUI. You do not have to provide any functionality.
SOLUTION
import java.awt.*;
import javax.swing.*;
// declaration of super class
class DrawPanel extends JPanel{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
//font wheel appearance
g.drawOval(100,200,90,90);
g.drawOval(110,210,70,70);
g.setColor(Color.black);
g.fillOval(138,237,14,14);
//smoke appearance
g.drawOval(480,240,10,8);
g.drawOval(487,233,10,11);
g.drawOval(498,170,20,31);
g.drawOval(570,120,20,31);
//back wheel appearance
g.drawOval(300,200,90,90);
g.drawOval(310,210,70,70);
g.fillOval(338,237,14,14);
//bottom line middle appearance
g.drawLine(190,244,300,244);
g.drawLine(194,234,296,234);
//bottom line front appearance
g.drawLine(100,244,34,244);
g.drawLine(96,234,40,234);
//bottom line back appearance
g.drawLine(394,234,460,234);
//vertical line font appearance
g.drawLine(34,244,50,185);
g.drawLine(41,234,60,188);
g.drawLine(96,234,120,188);
//vertical line font apearancea
g.drawLine(193,234,176,179);
//vertical line back appearance
g.drawLine(396,234,380,189);
//vertical line back appearance
g.drawLine(296,234,320,188);
g.drawLine(340,167,346,157);
g.setColor(Color.red);
//vertical line back;
g.drawLine(440,168,460,234);
//uper vertical line font appearance
g.drawLine(50,185,145,165);
//uper vertical line back
g.drawLine(345,155,440,168);
//font mirror appearance
g.drawLine(145,165,190,110);
g.drawLine(160,155,190,120);
//font mirror-horizontal line appearance
g.drawLine(160,155,240,155);
g.drawLine(190,120,240,120);
g.drawLine(240,155,240,120);
g.drawLine(268,155,268,120);
g.drawLine(268,120,305,120);
g.drawLine(268,155,320,155);
g.drawLine(320,155,306,120);
g.drawRect(210,160,27,7);
g.drawRect(294,160,27,6);
g.drawLine(390,250,410,250);
g.drawLine(390,240,410,240);
g.drawRect(410,238,35,15);
g.drawLine(445,250,460,250);
g.drawLine(445,240,460,240);
g.drawLine(440,239,440,233);
g.drawLine(415,239,415,233);
g.drawLine(320,180,330,160);
g.drawLine(85,230,95,210);
g.drawLine(76,230,86,210);
g.drawLine(56,230,66,210);
//back mirror appearance
g.drawLine(300,110,345,155);
//top horizontal line appearance
g.drawLine(190,110,300,110);
//middle holizontal-line back
g.drawLine(345,168,440,168);
//midde holizantal-line font appearance
g.drawLine(58,189,139,189);
g.drawLine(120,187,180,184);
g.drawLine(320,189,380,189);
//middle vertical line appaearance
g.drawLine(240,234,260,110);
}
public static void main(String[] args){
DrawPanel panel = new DrawPanel();
JPanel panel1=new JPanel();
panel1.setSize(600,30);
panel1.setBackground(Color.gray);
JLabel file=new JLabel("File".toUpperCase());
JLabel element=new JLabel("element".toUpperCase());
file.setHorizontalAlignment(JLabel.RIGHT);
element.setHorizontalAlignment(JLabel.CENTER);
file.setForeground(Color.white);
element.setForeground(Color.white);
JFrame application=new JFrame("sketcher".toUpperCase());
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel1.add(file);
panel1.add(element);
application.add(panel1);
application.add(panel);
application.setSize(600,600);
application.setVisible(true);
}
}