NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package lab4;

import java.util.Scanner;

/**
*
* @author FSKTM
*/
public class L1Q1 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
System.out.print("Enter an Integer: ");
Scanner scan = new Scanner(System.in);
int num = scan.nextInt();
System.out.print("The factors are: ");
for (int i = 1; i <= num; i++)
{
if (num % i == 0)
{
System.out.print(i+", ");
}
}
}
}

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package lab4;

import java.util.ArrayList;
import java.util.Random;

/**
*
* @author FSKTM
*/
public class L1Q2
{
public static void main(String[] args)
{
// Set Biggest prime number
int max=1000;

// Sieve of Erastos
boolean[] isPrime = new boolean[max+1];
for(int i=2; i<=max; i++)
{
isPrime[i] = true;
}

// set nonprime to false
for(int factor=2; factor*factor <= max; factor++)
{
for(int j=factor; factor*j <= max; j++)
{
isPrime[j*factor] = false;
}
}

// Put all primes in a list
ArrayList prime = new ArrayList();
for(int i=0; i<=max;i++)
{
if(isPrime[i])
{
prime.add(i);
}
}

System.out.println(prime.toString());

int randInt = new Random().nextInt(99) + 1; // 1~99
System.out.printf("The %d-th prime is: %dn", randInt, prime.get(randInt-1));





}

}


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package lab4;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

/**
*
* @author FSKTM
*/
public class L1Q3
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
ArrayList<Integer> inputInts = new ArrayList<Integer>();
while (true)
{
System.out.print("Enter a score [negative score to quit]:");
int inputInt = input.nextInt();
if (inputInt <0)
{
break;
}
else
{
inputInts.add(inputInt);
}
}

int max = Collections.max(inputInts);
int min = Collections.min(inputInts);
int sum = 0;
int sumSquared = 0;
for(int i=0; i<inputInts.size(); i++)
{
int num = inputInts.get(i);
sum += num;
sumSquared += Math.pow(num, 2);
}
int N = inputInts.size();
double avr = (double)sum / inputInts.size();
double stdDev = Math.sqrt( (sumSquared - Math.pow(sum, 2) / (double) N ) / (N-1) );

System.out.printf("Max: %d "
+ "Min: %d "
+ "Sum: %d "
+ "Avr: %.2f "
+ "StdDev: %.2f"
+ "n", max, min, sum, avr, stdDev
);
}
}

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package lab4;

import java.util.Random;
import java.util.Scanner;

/**
*
* @author FSKTM
*/
public class L1Q4
{

public static void main(String[] args)
{
int score1 = 0;
int score2 = 0;
int winScore = 101;

Random rand = new Random();
Scanner input = new Scanner(System.in);


while (score1 < 100 && score2 < 100)
{
int die1 = 0;
do
{
if (die1 == 6)
System.out.println("Congrats, P1! You've got a 6! Extra roll!");
System.out.print("P1, please <Enter> to roll: ");
input.nextLine();
die1 = rand.nextInt(6) + 1;
System.out.printf("You roll a [%d]!n", die1);
score1 += die1;
System.out.printf("[P1: %d] P2: %dnn", score1, score2);
if (score1 >= winScore)
{
System.out.println("P1 wins!");
return;
}
} while (die1 == 6);

int die2 = 0;
do
{
if (die2 == 6)
System.out.println("Congrats, P2! You've got a 6! Extra roll!");
System.out.print("P2, please <Enter> to roll: ");
input.nextLine();
die2 = rand.nextInt(6) + 1;
System.out.printf("You roll a [%d]!n", die2);
score2 += die2;
System.out.printf("P1: %d [P2: %d]nn", score1, score2);
if (score2 >= winScore)
{
System.out.println("P2 wins!");
return;
}
} while (die2 == 6);
}
}
}

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package lab4;

import java.util.Random;

/**
*
* @author FSKTM
*/
public class L1Q5
{
public static void main(String[] args)
{
final int MAX_DIGIT = 8;
Random rand = new Random();
int number = rand.nextInt( (int)Math.pow(10, MAX_DIGIT) );
System.out.printf("Number: %dn", number);
int digitCount = 0;
while(number > 0)
{
digitCount ++;
number = number / 10;
}
System.out.printf("No. digit: %2d n", digitCount);
}
}

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package lab4;

import java.util.Scanner;

/**
*
* @author FSKTM
*/
public class L1Q6
{
public static void main(String[] args)
{
int N; // Total num months
double M, // Monthly payment
P, // Principal
i, // yearly interest in %
Cn, // Principal portion due
Ln, // interest due
Rn, // remaining principal balance due
Ti = 0; // total interest

// Get inputs
Scanner input = new Scanner(System.in);
System.out.print("Enter principal amount: ");

// P = input.nextDouble();
System.out.print("Enter interest in %: ");
// i = input.nextDouble();
System.out.print("Enter total number of month(s): ");
// N = input.nextInt();

// Test
System.out.println("");
P = 1000; i=4; N=12;

// Print table title
// System.out.println("MonthtMonthly PaymenttPrincipaltInterestt"
// + "Unpaid BalancetTotal Interest");
System.out.printf("%-10s%15s%15s%15s%15s%15s%n",
"Month",
"Monthly Payment",
"Principal",
"Interest",
"Unpaid Balance",
"Total Interest");
// Print every month in row
for (int n=1; n <= N; n++)
{
M = ( P * i/(12*100) ) / ( 1 - Math.pow(1 + i/(12*100) , -N) );
Cn = M * Math.pow( 1 + i/(12*100), -(1+N-n) );
Ln = M - Cn;
Rn = Ln /( i/(12*100) ) - Cn;
Ti += Ln;

System.out.printf("%-10d%15f%15f%15f%15f%15f%n",
n, M, Cn, Ln, Rn, Ti);
}

}
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.