Vous êtes sur la page 1sur 12

QSN 1.

(a)

public class Myclass{

public void progation1 throws ArithmeticException(){

throw new ArithmeticException();

public void progation2 throws ArithmeticException(){

progatoin1();

public class Myclass1{

public static void main(String args[]){

try{

progation2();

catch(ArithmeticException e){

System.out.println(e);

(b) 20

__________________________________________________________________________________
_________________

QSN 2

(a)

package aevent;

import java.awt.Color;
import java.awt.event.*;

import javax.swing.*;

public class Text implements ActionListener{

JTextArea area;

JTextArea area1;

JButton area2;

JLabel area3;

JLabel area4;

JFrame f;

Text(){

f = new JFrame();

area = new JTextArea();

area1 = new JTextArea();

area3 = new JLabel("celcius:");

area4 = new JLabel("farenheit:");

area2 = new JButton("Convert");

area3.setBounds(40,30,60,30);

area4.setBounds(40,80,60,30);

area2.setBounds(50,130,300,30);

area.setBounds(110,30,200,30);

area.setBackground(Color.red);

area1.setBounds(110,80,200,30);

area1.setBackground(Color.red);

area2.addActionListener(this);

f.add(area);

f.add(area1);

f.add(area2);

f.add(area3);

f.add(area4);

f.setSize(400,400);

f.setLayout(null);
f.setVisible(true);

public void actionPerformed(ActionEvent e){

try{

int tempFahr = (int) ((Double.parseDouble(area.getText())) * 0.2432432);

area1.setText(tempFahr + " Fahrenheit");

}catch(Exception r){

System.out.println("invalid user input");

public static void main(String args[]){

new Text(); }}

(b). Because abstract means implements no functionarity and static means There is functionality
evenif you dont have an object instance and that is a logical contradiction.

__________________________________________________________________________________
____________________________________

QSN 3.

(a)

ackage savingaccount;

import java.util.Scanner;

public class SavingAccount {

public static double annualInterestRate;

SavingAccount(){

annualInterestRate = 0;

public static double modifyInteretRate(double rate){


annualInterestRate = rate;

return rate;

public void calculateMonthlyInterest(double savingBalance){

savingBalance = savingBalance+((savingBalance*annualInterestRate)/12);

System.out.println("The new Balance is "+savingBalance);

package savingaccount;

import java.util.Scanner;

public class Test {

public static void main(String args[]){

double rate;

System.out.println("Enter the interest rate");

Scanner input = new Scanner(System.in);

rate= input.nextInt();

SavingAccount x1 = new SavingAccount();

SavingAccount server1 = new SavingAccount();

SavingAccount server2 = new SavingAccount();

x1.modifyInteretRate(rate);

server1.calculateMonthlyInterest(20000);

server2.calculateMonthlyInterest(30000);

(B). methode sum with two parameters integers has arleady defined in class calculation
__________________________________________________________________________________
____________________

QSN 4.

(a). The university of Dodoma

(b).

package userpromt;

public class UserPromt {

public static void main(String[] args) {

class2 num = new class2();

try{

num.Numbers();

}catch(Exception e)

System.out.println(e);

}
package userpromt;

import java.util.Scanner;

public class class2 {

public int summation;

public int product;

public int average;

class2(){

summation = 0;

product = 1;

average = 0;

public void Numbers() throws Exception{

int num[] = new int[3];

int avr,sum ;

int product;

avr = average;

sum = summation;

int largest = 0;

int smallest = 0;

int odd=0;

int even=0;

product = this.product;

Scanner input = new Scanner(System.in);

for(int i=0; i <num.length; i++){

System.out.print("Enter an interger");

num[i]= input.nextInt();

if(num[i]<=0){

throw new Exception("Error The integer must be greater greater than zero");

}
else{

//count an odd and even numbers

if((num[i]%2) == 0){

even = even+1;

else{

odd = odd+1;

//to find smallest and largest number

largest = num[0];

smallest = num[0];

if(num[i]>largest){

largest = num[i];

else if(num[i]<smallest){

smallest = num[i];

sum = sum+num[i];

product = product*num[i];

avr = sum/num.length;

System.out.println("Sum = "+sum);

System.out.println("product = "+product);

System.out.println("average = "+avr);

System.out.println("largest number is = "+largest);

System.out.println("smallest number is = "+smallest);

System.out.println("there is "+even+" number of even");

System.out.println("there is "+odd+" number of odd");


checkProduct(product);

checkSum(sum);

public void checkProduct(int product){

if(product<100){

System.out.println("product is less than 100");

else if(product==100){

System.out.println("product is equal to 100");

else{

System.out.println("product is greater than 100");

public void checkSum(int sum){

if(sum<100){

System.out.println("sum is less than 100");

else if(sum==100){

System.out.println("sum is equal to 100");

else{

System.out.println("sum is greater than to 100");

__________________________________________________________________________________
___________________________
QSN 5.

package theedimensionshape;

public interface TheeDimensionShape {

public abstract double CalculateArea(int x);

public abstract double CalculateVolume(int y);

package theedimensionshape;

public class Sphere implements TheeDimensionShape{

@Override

public double CalculateArea(int radius) {

double area;

area = 4*3.14*radius*radius;

return area;

@Override

public double CalculateVolume(int radius) {

double volume;

volume = (4/3)*3.14*radius*radius*radius;

return volume;

}
package theedimensionshape;

public class Cube implements TheeDimensionShape{

public double CalculateArea(int side) {

double area;

area = 6*side*side;

return area;

public double CalculateVolume(int side) {

double volume;

volume = side*side*side;

return volume;

package theedimensionshape;

public class Test {

public static void main(String args[]){

TheeDimensionShape x1 = new Sphere();

TheeDimensionShape x2 = new Cube();

System.out.println("Area of the sphere is "+x1.CalculateArea(5)+" cm sqre");

System.out.println("volume of the sphere is "+x1.CalculateVolume(5)+" cm sqre");

System.out.println("Area of the Cube is "+x2.CalculateArea(3)+" cm cubic");

System.out.println("Volume of Cube is "+x2.CalculateVolume(3)+" cm cubic");

}
__________________________________________________________________________________
_________________________________________________-

QSN 6.

(a)

import java.sql.*;

public class DbConnection {

static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";

static final String DB_URL = "jdbc:mysql://localhost/EmployeeDB";

static final String USER = "root";

static final String PASS = "root";

public static void main(String[] args) {

Connection conn = null;

Statement stmt = null;

try{

Class.forName("com.mysql.jdbc.Driver");

System.out.println("Connecting to database...");

conn = DriverManager.getConnection(DB_URL,USER,PASS);

System.out.println("Creating statement...");

stmt = conn.createStatement();

String sql;

sql = "SELECT first_name, last_name, phone_number FROM EmployeeInfo";

ResultSet rs = stmt.executeQuery(sql);

while(rs.next()){

//Retrieve by column name

String first = rs.getString("first_name");


String last = rs.getString("last_name");

int phone = rs.getInt("phone_number");

System.out.print(", First name: " + first);

System.out.println(", Last name: " + last);

System.out.print(", phone number: " + phone);

(b). True

Vous aimerez peut-être aussi