0% found this document useful (0 votes)
3 views24 pages

STEV

The document contains a series of programming tasks primarily focused on Java, including creating a Rectangle class with methods for area and perimeter, working with a 3-by-4 integer array, calculating a cell phone bill based on usage, defining an abstract Shape class with derived classes, and creating a GUI. Each task includes code snippets and explanations for implementation. The document serves as a comprehensive guide for Java programming exercises.

Uploaded by

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

STEV

The document contains a series of programming tasks primarily focused on Java, including creating a Rectangle class with methods for area and perimeter, working with a 3-by-4 integer array, calculating a cell phone bill based on usage, defining an abstract Shape class with derived classes, and creating a GUI. Each task includes code snippets and explanations for implementation. The document serves as a comprehensive guide for Java programming exercises.

Uploaded by

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

Tasks1.

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:

//java program for calculate area and perimeter of rectangle


import java.util.Scanner;
//the super class
public class Qn1{
//starting main method
public static void main(String[] args){
//Declare variable length and width

float length=2, width=2;

//scanner to obtain input


Scanner in=new Scanner(System.in);

System.out.println("Enter length ranging 10.00 to 40.00: ");//Allow


user to inter the length
length=in.nextFloat();

System.out.println("Enter width ranging 10.00 to 40.00: ");//Allow


user to inter the width
width=in.nextFloat();

//condition to check if the length and width is on the range given


if(width>10.00 &&width<=40.00){
if(length>10.00 &&length<=40.00){
RectangleTest Object=new RectangleTest();
Object.setArea(length,width);
Object.setPerim(length,width);
Object.getArea();
Object.getPerim();
}
}
else{
System.out.println("length and width entered are not in range of 10.00
to 40.00");
System.out.println("length and width should default to 2");
//the defaults values
length=2; width=2;
RectangleTest Object=new RectangleTest();
//passing variabes
Object.setArea(length,width);
Object.setPerim(length,width);
//calling output
Object.getArea();
Object.getPerim();
}
}
}

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

Complete code for above implementation is:

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 row and column


for(i=1;i<3;i++){
}
System.out.println("There are rows: "+i);
for(j=1;j<4;j++){
}
System.out.println("There are column: "+j);

//Counting element of k
count=i*j;
System.out.println("There are element: "+count);

//Accessing expression of row 3 in array


System.out.printf("Element of row 3 are: %d %d %d %d \n",k[2][0], k[2][1], k[2][2], k[2]
[3]);

//seting the element of k in row 0 and column 1 to zero


System.out.println("element of k in row 0 and column 1 has been set to zero");
k [0][1]=0;
for(i=0; i<3;i++){
for(j=0;j<4;j++){
System.out.printf(" "+k[i][j]);
}
System.out.printf("\n");
}

//individual statements to initialize each element of k to zero.


System.out.println("individual statements to initialize each element of k to zero");
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;
k [2][0]=0;
k [2][1]=0;
k [2][2]=0;
k [2][3]=0;
for(i=0; i<3;i++){
for(j=0;j<4;j++){
count++;
System.out.printf(" "+k[i][j]);
}
System.out.printf("\n");
}

//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");
}

//statements that determines and displays the smallest value in k


int min = k[0][0];
for ( i = 0; i <3; i++ ){
for ( j = 0; j <4; j++ ){
if ( k[i][j] < min ){
min = k[ i ][ j ];
}
}
}
System.out.println("The smallest element in k is: "+min);

//statement that totals the elements of the third column of k


int sum = k[0][2]+k[1][2]+k[2][2];
System.out.println("Total of element of the third column of k is: "+sum);

//statements that displays the contents of t in tabular format


System.out.println("contents of t in tabular format");
System.out.println("col col col col");
System.out.println(" 0 1 2 3");
for(i=0; i<3;i++){
System.out.print(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.*;

public class Question4{

public static void main(String[]args){

Scanner sc=new Scanner(System.in);

Lines L1=new Lines(10,20,12,13);

Rectangles r1=new Rectangles(12,10.5,6.8,13.4);

Circles c1=new Circles(10.2,20.2,12);

PolyLines pl1=new PolyLines(12,10.56,8,13.4,11.5,11.4);

int ch=0;
double dx=0;

double dy=0;

while(true)

System.out.println("Menu Driven-To move");

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");

System.out.println("Enter your choice:");

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;

public class Lines extends Shape{


private double x2;

private double y2;

public Lines(double x1, double y1, double x2, double y2)

this.x1=x1;

this.y1=y1;

this.x2=x2;

this.y2=y2;

public void move(double dx, double dy)

super.move(dx,dy);

this.x2=this.x2+dx;

this.y2=y2+dy;

public String show(){


return String.format("Line coordinates%nX=%f"+"y1=%f%nX2=%fy2=%f",x1,y1,x2,y2);

public String tostring()

return show();

@ Override

public String how(){

return null;

//Circle

import shapi.Shape;

public class Circles extends Shape{

private double r;

Circles(double x1,double y1,double r)

this.x1=x1;

this.y1=y1;
this.r=r;

public void move(double dx, double dy)

super.move(dx,dy);

public String show()

return String.format("circle coordinates %nCenter(x1=%f"+"y1=%f)%nRadius=%f",x1,y1,r);

public String toString()

return show();

@ Override

public String how(){

return null;

// Rectangle

import shapi.Shape;
public class Rectangles extends Shape{

private double x2;

private double y2;

Rectangles(double x1,double y1,double x2, double y2)

this.x1=x1;

this.y1=y1;

this.x2=x2;

this.y2=y2;

public void move(double dx,double dy)

super.move(dx,dy);

this.x2=this.x2+dx;

this.y2=this.y2+dy;

public String show()

return String.format("Rectangle coordinates %nDiagonal Opposite Corners %n"+"x1=


%f"+"y1=%f%nx2=%f y2=%f",x1,y1,x2,y2);

public String toString()


{

return show();

@ Override

public String how(){

return null;

//polylines

import shapi.Shape;

public class PolyLines extends Shape{

private double x2;

private double y2;

private double x3;

private double y3;

PolyLines(double x1,double y1,double x2,double y2,double x3,double y3){

this.x1=x1;

this.y1=y1;

this.x2=x2;

this.y2=y2;

this.x3=x3;
this.y3=y3;

public void move(double dx,double dy)

super.move(dx,dy);

this.x2=this.x2+dx;

this.y2=this.y2+dy;

this.x3=this.x3+dx;

this.y3=this.y3+dy;

public String show()

return string.format("polyline coordinates%n"+"x1=%f"+"y1=%f %nx2=%f y2=%f %nx3=


%f",x1,y1,x2,y2,x3,y3);

public String toString()

return Show();

@ override

public String how(){

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);
}
}

You might also like