Assigment # 01:: SIR Hasnat ALI
Assigment # 01:: SIR Hasnat ALI
Assigment # 01:: SIR Hasnat ALI
FACULTY OF COMPUTING
RIPHAH INTERNATIONAL UNIVERSITY
Assigment # 01
Submitted by SAP ID.
Majid Hussain 2223
DATE : 27-10-2020
1|Page
ADVANCED COMPUTER PROGRAMMING
Contents
Ans1: ....................................................................................................................................................... 3
Ans2: ....................................................................................................................................................... 5
Ans3: ....................................................................................................................................................... 7
Ans4: ....................................................................................................................................................... 9
Ans5: ..................................................................................................................................................... 11
Ans6: ..................................................................................................................................................... 13
Ans7: ..................................................................................................................................................... 15
2|Page
ADVANCED COMPUTER PROGRAMMING
Tasks:
SOLUTION:
CODE:
Code
package q1;
import java.util.Scanner;
public class Q1 {
public static int hex_to_decimal(String maj)
{
String digits = "0123456789ABCDEF";
maj = maj.toUpperCase();
int val = 0;
for (int i = 0; i < maj.length(); i++)
{
char c = maj.charAt(i);
int d = digits.indexOf(c);
val = 16*val + d;
}
return val;
}
public static void main(String args[])
{
String hexadec_num;
int deci_num;
Scanner scan = new Scanner(System.in);
System.out.print("Enter a hexadecimal number: ");
hexadec_num = scan.nextLine();
deci_num = hex_to_decimal(hexadec_num);
System.out.print("Equivalent decimal number is: " + deci_num+"\n");
}
}
3|Page
ADVANCED COMPUTER PROGRAMMING
CMD OUTPUT:
4|Page
ADVANCED COMPUTER PROGRAMMING
2. Write a method that finds the smallest element in an array of double values using the
SOLUTION:
CODE:
Code
package q2;
import java.util.Scanner;
public class Q2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double[] numbers = new double[5];
System.out.print("Enter Five numbers: ");
for (int i = 0; i < numbers.length; i++) {
numbers[i] = input.nextDouble();
}
System.out.println("The minimum number is: " + min(numbers));
}
public static double min(double[] array) {
double min = array[0];
for (double i: array) {
if (i < min)
min = i;
}
return min;
}
}
5|Page
ADVANCED COMPUTER PROGRAMMING
CMD OUTPUT:
6|Page
ADVANCED COMPUTER PROGRAMMING
SOLUTION:
CODE:
Code
package q3;
public class Q3 {
public static boolean sudokuCheck(int[][] s)
{
for(int r1=0;r1<9;r1++)
for(int c1=0;c1<8;c1++)
if(s[r1][c1]==s[r1][c1+1])
{
return false;
}
for(int c2=0;c2<9;c2++)
for(int r2=0;r2<8;r2++)
if (s[r2][c2]==s[r2+1][c2])
return false;
return true;
}
public static void main (String[] args)
{
int[][] solution = {{5,1,6,4,7,2,9,3,8}, {4,2,9,8,1,3,5,6,7},
{6,7,2,1,3,4,8,5,9}, {7,8,3,6,5,9,2,4,1},
{8,4,1,7,9,5,6,2,3}, {3,9,5,2,8,6,1,7,4},
{9,3,4,5,2,8,7,1,6}, {1,5,8,3,6,7,4,9,2},
{2,6,7,9,4,1,3,8,5}};
if(sudokuCheck(solution)==true)
System.out.println("Sudoku is Correct.");
else
System.out.println(" Sudoku is not Correct.");
}
}
7|Page
ADVANCED COMPUTER PROGRAMMING
CMD OUTPUT:
8|Page
ADVANCED COMPUTER PROGRAMMING
4. Write a program to sort the names in an array in alphabetical order. Accept inputs
from users to populate the array with 10 names. The program should print the sorted
array as an output.
SOLUTION:
CODE:
Code
package q4;
import java.util.Scanner;
public class Q4 {
public static void main(String[] args)
{
int count;
String t1;
Scanner scan = new Scanner(System.in);
System.out.print("Enter number of strings you enter:");
count = scan.nextInt();
String str[] = new String[count];
Scanner scan2 = new Scanner(System.in);
System.out.println("Enter the Strings:");
for(int i = 0; i < count; i++)
{
str[i] = scan2.nextLine();
}
scan.close();
scan2.close();
for (int i = 0; i < count; i++)
{
for (int j = i + 1; j < count; j++) {
if (str[i].compareTo(str[j])>0)
{
t1 = str[i];
str[i] = str[j];
str[j] = t1;
}
}
}
}
System.out.print("\n----------------------------------");
}
9|Page
ADVANCED COMPUTER PROGRAMMING
CMD OUTPUT:
10 | P a g e
ADVANCED COMPUTER PROGRAMMING
5. Use method overloading to populate the array with double numbers. Write a program
to sort the numbers in an array in ascending order. Accept inputs from users to populate
the array with 10 numbers. The program should print the sorted array as an output.
SOLUTION:
CODE:
Code
package q5;
import java.util.Scanner;
public class Q5 {
public static void main(String[] args)
{
int majid, t1;
Scanner scan = new Scanner(System.in);
System.out.print("Enter number of Array elements: ");
majid = scan.nextInt();
11 | P a g e
ADVANCED COMPUTER PROGRAMMING
CMD OUTPUT:
12 | P a g e
ADVANCED COMPUTER PROGRAMMING
a. Two double data fields named width and height that specify the width and height of
the rectangle. The default values are 1 for both width and height.
c. A constructor that creates a rectangle with the specified width and height.
SOLUTION:
CODE:
Code
package q6;
public class Q6 {
public static void main(String[] args){
Rect r1 = new Rect();
Rect r2 = new Rect(4, 40);
Rect r3 = new Rect(3.5, 35.9);
System.out.print("-------------------------------------------------\n");
System.out.println("The perimeter of the first box is: " + r1.getPerimeter() +
"\n");
System.out.println("The perimeter of the second box is: " + r2.getPerimeter() +
"\n");
System.out.println("The perimeter of the third box is: " + r3.getPerimeter() +
"\n");
System.out.println("The area of the first box is: " + r1.getArea() + "\n");
System.out.println("The area of the second box is: " + r2.getArea() + "\n");
System.out.println("The area of the third box is: " + r3.getArea() + "\n");
System.out.print("-------------------------------------------------\n");
}
}
class Rect {
double height;
double width;
13 | P a g e
ADVANCED COMPUTER PROGRAMMING
width = 1;
}
public void setHeight(double high){
height = high;
}
public void setWidth(double wid){
width = wid;
}
14 | P a g e
ADVANCED COMPUTER PROGRAMMING
CMD OUTPUT:
7. Write a program that creates a Date object, sets its elapsed time to 10000, 100000,
1000000, 10000000, 100000000, 1000000000, 10000000000, and 100000000000, and
displays the date and time using the toString() method, respectively.
SOLUTION:
CODE:
Code
package q7;
import java.util.Date;
public class Q7 {
public static void main(String[] args) {
Date date;
long time = 10000;
for (int i = 0; i < 8; i++, time *= 10) {
date = new Date(time);
System.out.println(date.toString());
}
}
}
15 | P a g e
ADVANCED COMPUTER PROGRAMMING
CMD OUTPUT:
16 | P a g e